blob: c7b1c37fdbac276d29e17d573635e41620d194db [file] [log] [blame]
Potin Lai5d214202023-01-16 18:11:49 +08001#include "config.h"
2
Patrick Venture0b02be92018-08-31 11:55:55 -07003#include <arpa/inet.h>
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05304#include <fcntl.h>
Xo Wang87651332017-08-11 10:17:59 -07005#include <limits.h>
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05306#include <linux/i2c-dev.h>
7#include <linux/i2c.h>
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05308#include <sys/ioctl.h>
9#include <sys/stat.h>
10#include <sys/types.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070011#include <systemd/sd-bus.h>
Xo Wang87651332017-08-11 10:17:59 -070012#include <unistd.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070013
Vernon Mauery0120b682019-03-25 13:08:54 -070014#include <app/channel.hpp>
15#include <app/watchdog.hpp>
16#include <apphandler.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050017#include <ipmid/api.hpp>
18#include <ipmid/sessiondef.hpp>
19#include <ipmid/sessionhelper.hpp>
20#include <ipmid/types.hpp>
21#include <ipmid/utils.hpp>
22#include <nlohmann/json.hpp>
23#include <phosphor-logging/elog-errors.hpp>
George Liu24fffdc2024-07-17 17:40:53 +080024#include <phosphor-logging/lg2.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050025#include <sdbusplus/message/types.hpp>
26#include <sys_info_param.hpp>
27#include <xyz/openbmc_project/Common/error.hpp>
28#include <xyz/openbmc_project/Control/Power/ACPIPowerState/server.hpp>
29#include <xyz/openbmc_project/Software/Activation/server.hpp>
30#include <xyz/openbmc_project/Software/Version/server.hpp>
31#include <xyz/openbmc_project/State/BMC/server.hpp>
32
33#include <algorithm>
Patrick Venture3a5071a2018-09-12 13:27:42 -070034#include <array>
Willy Tu886d6842022-06-03 02:50:47 -070035#include <charconv>
Patrick Venture3a5071a2018-09-12 13:27:42 -070036#include <cstddef>
Vernon Mauery0120b682019-03-25 13:08:54 -070037#include <cstdint>
Vernon Mauerybdda8002019-02-26 10:18:51 -080038#include <filesystem>
Patrick Venture3a5071a2018-09-12 13:27:42 -070039#include <fstream>
40#include <memory>
Potin Lai5d214202023-01-16 18:11:49 +080041#include <regex>
Patrick Venture3a5071a2018-09-12 13:27:42 -070042#include <string>
Willy Tu886d6842022-06-03 02:50:47 -070043#include <string_view>
Patrick Venture3a5071a2018-09-12 13:27:42 -070044#include <tuple>
45#include <vector>
Ratan Guptab8e99552017-07-27 07:07:48 +053046
Patrick Venture0b02be92018-08-31 11:55:55 -070047extern sd_bus* bus;
vishwabmcba0bd5f2015-09-30 16:50:23 +053048
Alexander Amelkinba19c182018-09-04 15:49:36 +030049constexpr auto bmc_state_interface = "xyz.openbmc_project.State.BMC";
50constexpr auto bmc_state_property = "CurrentBMCState";
Marri Devender Rao5e007a52018-01-08 06:18:36 -060051
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060052static constexpr auto redundancyIntf =
53 "xyz.openbmc_project.Software.RedundancyPriority";
Patrick Venture0b02be92018-08-31 11:55:55 -070054static constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060055static constexpr auto activationIntf =
56 "xyz.openbmc_project.Software.Activation";
57static constexpr auto softwareRoot = "/xyz/openbmc_project/software";
58
Chris Austen6caf28b2015-10-13 12:40:40 -050059void register_netfn_app_functions() __attribute__((constructor));
vishwabmcba0bd5f2015-09-30 16:50:23 +053060
Ratan Guptab8e99552017-07-27 07:07:48 +053061using namespace phosphor::logging;
Willy Tu523e2d12023-09-05 11:36:48 -070062using namespace sdbusplus::error::xyz::openbmc_project::common;
63using Version = sdbusplus::server::xyz::openbmc_project::software::Version;
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060064using Activation =
Willy Tu523e2d12023-09-05 11:36:48 -070065 sdbusplus::server::xyz::openbmc_project::software::Activation;
66using BMC = sdbusplus::server::xyz::openbmc_project::state::BMC;
Vernon Mauery185b9f82018-07-20 10:52:36 -070067namespace fs = std::filesystem;
Ratan Guptab8e99552017-07-27 07:07:48 +053068
Yong Libd0503a2019-08-22 17:17:17 +080069#ifdef ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053070typedef struct
71{
72 uint8_t busId;
Matt Simmering68d9d402023-11-09 14:22:11 -080073 uint8_t targetAddr;
74 uint8_t targetAddrMask;
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053075 std::vector<uint8_t> data;
76 std::vector<uint8_t> dataMask;
Matt Simmering68d9d402023-11-09 14:22:11 -080077} i2cControllerWRAllowlist;
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053078
Matt Simmering68d9d402023-11-09 14:22:11 -080079static std::vector<i2cControllerWRAllowlist>& getWRAllowlist()
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053080{
Matt Simmering68d9d402023-11-09 14:22:11 -080081 static std::vector<i2cControllerWRAllowlist> wrAllowlist;
82 return wrAllowlist;
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053083}
84
Matt Simmering68d9d402023-11-09 14:22:11 -080085static constexpr const char* i2cControllerWRAllowlistFile =
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053086 "/usr/share/ipmi-providers/master_write_read_white_list.json";
87
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053088static constexpr const char* filtersStr = "filters";
89static constexpr const char* busIdStr = "busId";
Matt Simmering68d9d402023-11-09 14:22:11 -080090static constexpr const char* targetAddrStr = "slaveAddr";
91static constexpr const char* targetAddrMaskStr = "slaveAddrMask";
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053092static constexpr const char* cmdStr = "command";
93static constexpr const char* cmdMaskStr = "commandMask";
94static constexpr int base_16 = 16;
Yong Libd0503a2019-08-22 17:17:17 +080095#endif // ENABLE_I2C_WHITELIST_CHECK
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +000096static constexpr uint8_t oemCmdStart = 192;
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +000097static constexpr uint8_t invalidParamSelectorStart = 8;
98static constexpr uint8_t invalidParamSelectorEnd = 191;
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053099
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600100/**
101 * @brief Returns the Version info from primary s/w object
102 *
103 * Get the Version info from the active s/w object which is having high
104 * "Priority" value(a smaller number is a higher priority) and "Purpose"
105 * is "BMC" from the list of all s/w objects those are implementing
106 * RedundancyPriority interface from the given softwareRoot path.
107 *
108 * @return On success returns the Version info from primary s/w object.
109 *
110 */
Vernon Maueryea1c4012019-05-24 13:26:16 -0700111std::string getActiveSoftwareVersionInfo(ipmi::Context::ptr ctx)
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600112{
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600113 std::string revision{};
Vernon Mauery86a50822019-03-25 13:11:36 -0700114 ipmi::ObjectTree objectTree;
115 try
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600116 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500117 objectTree = ipmi::getAllDbusObjects(*ctx->bus, softwareRoot,
118 redundancyIntf);
Vernon Mauery86a50822019-03-25 13:11:36 -0700119 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500120 catch (const sdbusplus::exception_t& e)
Vernon Mauery86a50822019-03-25 13:11:36 -0700121 {
George Liu24fffdc2024-07-17 17:40:53 +0800122 lg2::error("Failed to fetch redundancy object from dbus, "
123 "interface: {INTERFACE}, error: {ERROR}",
124 "INTERFACE", redundancyIntf, "ERROR", e);
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600125 elog<InternalFailure>();
126 }
127
128 auto objectFound = false;
129 for (auto& softObject : objectTree)
130 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500131 auto service = ipmi::getService(*ctx->bus, redundancyIntf,
132 softObject.first);
133 auto objValueTree = ipmi::getManagedObjects(*ctx->bus, service,
134 softwareRoot);
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600135
136 auto minPriority = 0xFF;
137 for (const auto& objIter : objValueTree)
138 {
139 try
140 {
141 auto& intfMap = objIter.second;
142 auto& redundancyPriorityProps = intfMap.at(redundancyIntf);
143 auto& versionProps = intfMap.at(versionIntf);
144 auto& activationProps = intfMap.at(activationIntf);
Vernon Maueryf442e112019-04-09 11:44:36 -0700145 auto priority =
146 std::get<uint8_t>(redundancyPriorityProps.at("Priority"));
William A. Kennington III4c008022018-10-12 17:18:14 -0700147 auto purpose =
Vernon Maueryf442e112019-04-09 11:44:36 -0700148 std::get<std::string>(versionProps.at("Purpose"));
149 auto activation =
150 std::get<std::string>(activationProps.at("Activation"));
William A. Kennington III4c008022018-10-12 17:18:14 -0700151 auto version =
Vernon Maueryf442e112019-04-09 11:44:36 -0700152 std::get<std::string>(versionProps.at("Version"));
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600153 if ((Version::convertVersionPurposeFromString(purpose) ==
154 Version::VersionPurpose::BMC) &&
155 (Activation::convertActivationsFromString(activation) ==
156 Activation::Activations::Active))
157 {
158 if (priority < minPriority)
159 {
160 minPriority = priority;
161 objectFound = true;
162 revision = std::move(version);
163 }
164 }
165 }
166 catch (const std::exception& e)
167 {
George Liu24fffdc2024-07-17 17:40:53 +0800168 lg2::error("error message: {ERROR}", "ERROR", e);
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600169 }
170 }
171 }
172
173 if (!objectFound)
174 {
George Liu24fffdc2024-07-17 17:40:53 +0800175 lg2::error("Could not found an BMC software Object");
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600176 elog<InternalFailure>();
177 }
178
179 return revision;
180}
181
Alexander Amelkinba19c182018-09-04 15:49:36 +0300182bool getCurrentBmcState()
183{
Patrick Williams5d82f472022-07-22 19:26:53 -0500184 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Alexander Amelkinba19c182018-09-04 15:49:36 +0300185
186 // Get the Inventory object implementing the BMC interface
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500187 ipmi::DbusObjectInfo bmcObject = ipmi::getDbusObject(bus,
188 bmc_state_interface);
189 auto variant = ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first,
190 bmc_state_interface,
191 bmc_state_property);
Alexander Amelkinba19c182018-09-04 15:49:36 +0300192
Vernon Maueryf442e112019-04-09 11:44:36 -0700193 return std::holds_alternative<std::string>(variant) &&
194 BMC::convertBMCStateFromString(std::get<std::string>(variant)) ==
195 BMC::BMCState::Ready;
Alexander Amelkinba19c182018-09-04 15:49:36 +0300196}
197
Patrick Venture94930a12019-04-30 10:01:58 -0700198bool getCurrentBmcStateWithFallback(const bool fallbackAvailability)
199{
200 try
201 {
202 return getCurrentBmcState();
203 }
204 catch (...)
205 {
206 // Nothing provided the BMC interface, therefore return whatever was
207 // configured as the default.
208 return fallbackAvailability;
209 }
210}
211
Yong Li18d77262018-10-09 01:59:45 +0800212namespace acpi_state
213{
Willy Tu523e2d12023-09-05 11:36:48 -0700214using namespace sdbusplus::server::xyz::openbmc_project::control::power;
Yong Li18d77262018-10-09 01:59:45 +0800215
216const static constexpr char* acpiObjPath =
217 "/xyz/openbmc_project/control/host0/acpi_power_state";
218const static constexpr char* acpiInterface =
219 "xyz.openbmc_project.Control.Power.ACPIPowerState";
220const static constexpr char* sysACPIProp = "SysACPIStatus";
221const static constexpr char* devACPIProp = "DevACPIStatus";
222
223enum class PowerStateType : uint8_t
224{
225 sysPowerState = 0x00,
226 devPowerState = 0x01,
227};
228
229// Defined in 20.6 of ipmi doc
230enum class PowerState : uint8_t
231{
232 s0G0D0 = 0x00,
233 s1D1 = 0x01,
234 s2D2 = 0x02,
235 s3D3 = 0x03,
236 s4 = 0x04,
237 s5G2 = 0x05,
238 s4S5 = 0x06,
239 g3 = 0x07,
240 sleep = 0x08,
241 g1Sleep = 0x09,
242 override = 0x0a,
243 legacyOn = 0x20,
244 legacyOff = 0x21,
245 unknown = 0x2a,
246 noChange = 0x7f,
247};
248
249static constexpr uint8_t stateChanged = 0x80;
250
Yong Li18d77262018-10-09 01:59:45 +0800251std::map<ACPIPowerState::ACPI, PowerState> dbusToIPMI = {
252 {ACPIPowerState::ACPI::S0_G0_D0, PowerState::s0G0D0},
253 {ACPIPowerState::ACPI::S1_D1, PowerState::s1D1},
254 {ACPIPowerState::ACPI::S2_D2, PowerState::s2D2},
255 {ACPIPowerState::ACPI::S3_D3, PowerState::s3D3},
256 {ACPIPowerState::ACPI::S4, PowerState::s4},
257 {ACPIPowerState::ACPI::S5_G2, PowerState::s5G2},
258 {ACPIPowerState::ACPI::S4_S5, PowerState::s4S5},
259 {ACPIPowerState::ACPI::G3, PowerState::g3},
260 {ACPIPowerState::ACPI::SLEEP, PowerState::sleep},
261 {ACPIPowerState::ACPI::G1_SLEEP, PowerState::g1Sleep},
262 {ACPIPowerState::ACPI::OVERRIDE, PowerState::override},
263 {ACPIPowerState::ACPI::LEGACY_ON, PowerState::legacyOn},
264 {ACPIPowerState::ACPI::LEGACY_OFF, PowerState::legacyOff},
265 {ACPIPowerState::ACPI::Unknown, PowerState::unknown}};
266
267bool isValidACPIState(acpi_state::PowerStateType type, uint8_t state)
268{
269 if (type == acpi_state::PowerStateType::sysPowerState)
270 {
271 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::override)) ||
272 (state == static_cast<uint8_t>(acpi_state::PowerState::legacyOn)) ||
273 (state ==
274 static_cast<uint8_t>(acpi_state::PowerState::legacyOff)) ||
275 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
276 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
277 {
278 return true;
279 }
280 else
281 {
282 return false;
283 }
284 }
285 else if (type == acpi_state::PowerStateType::devPowerState)
286 {
287 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::s3D3)) ||
288 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
289 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
290 {
291 return true;
292 }
293 else
294 {
295 return false;
296 }
297 }
298 else
299 {
300 return false;
301 }
302 return false;
303}
304} // namespace acpi_state
305
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000306/** @brief implements Set ACPI Power State command
307 * @param sysAcpiState - ACPI system power state to set
308 * @param devAcpiState - ACPI device power state to set
309 *
310 * @return IPMI completion code on success
311 **/
312ipmi::RspType<> ipmiSetAcpiPowerState(uint8_t sysAcpiState,
313 uint8_t devAcpiState)
Chris Austen6caf28b2015-10-13 12:40:40 -0500314{
Yong Li18d77262018-10-09 01:59:45 +0800315 auto s = static_cast<uint8_t>(acpi_state::PowerState::unknown);
Yong Li18d77262018-10-09 01:59:45 +0800316
Patrick Williams5d82f472022-07-22 19:26:53 -0500317 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Yong Li18d77262018-10-09 01:59:45 +0800318
319 auto value = acpi_state::ACPIPowerState::ACPI::Unknown;
320
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000321 if (sysAcpiState & acpi_state::stateChanged)
Yong Li18d77262018-10-09 01:59:45 +0800322 {
323 // set system power state
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000324 s = sysAcpiState & ~acpi_state::stateChanged;
Yong Li18d77262018-10-09 01:59:45 +0800325
326 if (!acpi_state::isValidACPIState(
327 acpi_state::PowerStateType::sysPowerState, s))
328 {
George Liu24fffdc2024-07-17 17:40:53 +0800329 lg2::error("set_acpi_power sys invalid input, S: {S}", "S", s);
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000330 return ipmi::responseParmOutOfRange();
Yong Li18d77262018-10-09 01:59:45 +0800331 }
332
333 // valid input
334 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
335 {
George Liu24fffdc2024-07-17 17:40:53 +0800336 lg2::debug("No change for system power state");
Yong Li18d77262018-10-09 01:59:45 +0800337 }
338 else
339 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500340 auto found = std::find_if(acpi_state::dbusToIPMI.begin(),
341 acpi_state::dbusToIPMI.end(),
342 [&s](const auto& iter) {
343 return (static_cast<uint8_t>(iter.second) == s);
344 });
Yong Li18d77262018-10-09 01:59:45 +0800345
346 value = found->first;
347
348 try
349 {
350 auto acpiObject =
351 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
352 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
353 acpi_state::acpiInterface,
354 acpi_state::sysACPIProp,
355 convertForMessage(value));
356 }
357 catch (const InternalFailure& e)
358 {
George Liu24fffdc2024-07-17 17:40:53 +0800359 lg2::error("Failed in set ACPI system property: {ERROR}",
360 "ERROR", e);
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000361 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800362 }
363 }
364 }
365 else
366 {
George Liu24fffdc2024-07-17 17:40:53 +0800367 lg2::debug("Do not change system power state");
Yong Li18d77262018-10-09 01:59:45 +0800368 }
369
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000370 if (devAcpiState & acpi_state::stateChanged)
Yong Li18d77262018-10-09 01:59:45 +0800371 {
372 // set device power state
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000373 s = devAcpiState & ~acpi_state::stateChanged;
Yong Li18d77262018-10-09 01:59:45 +0800374 if (!acpi_state::isValidACPIState(
375 acpi_state::PowerStateType::devPowerState, s))
376 {
George Liu24fffdc2024-07-17 17:40:53 +0800377 lg2::error("set_acpi_power dev invalid input, S: {S}", "S", s);
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000378 return ipmi::responseParmOutOfRange();
Yong Li18d77262018-10-09 01:59:45 +0800379 }
380
381 // valid input
382 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
383 {
George Liu24fffdc2024-07-17 17:40:53 +0800384 lg2::debug("No change for device power state");
Yong Li18d77262018-10-09 01:59:45 +0800385 }
386 else
387 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500388 auto found = std::find_if(acpi_state::dbusToIPMI.begin(),
389 acpi_state::dbusToIPMI.end(),
390 [&s](const auto& iter) {
391 return (static_cast<uint8_t>(iter.second) == s);
392 });
Yong Li18d77262018-10-09 01:59:45 +0800393
394 value = found->first;
395
396 try
397 {
398 auto acpiObject =
399 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
400 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
401 acpi_state::acpiInterface,
402 acpi_state::devACPIProp,
403 convertForMessage(value));
404 }
405 catch (const InternalFailure& e)
406 {
George Liu24fffdc2024-07-17 17:40:53 +0800407 lg2::error("Failed in set ACPI device property: {ERROR}",
408 "ERROR", e);
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000409 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800410 }
411 }
412 }
413 else
414 {
George Liu24fffdc2024-07-17 17:40:53 +0800415 lg2::debug("Do not change device power state");
Yong Li18d77262018-10-09 01:59:45 +0800416 }
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000417 return ipmi::responseSuccess();
Yong Li18d77262018-10-09 01:59:45 +0800418}
419
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000420/**
421 * @brief implements the get ACPI power state command
422 *
423 * @return IPMI completion code plus response data on success.
424 * - ACPI system power state
425 * - ACPI device power state
426 **/
427ipmi::RspType<uint8_t, // acpiSystemPowerState
428 uint8_t // acpiDevicePowerState
429 >
430 ipmiGetAcpiPowerState()
Yong Li18d77262018-10-09 01:59:45 +0800431{
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000432 uint8_t sysAcpiState;
433 uint8_t devAcpiState;
Yong Li18d77262018-10-09 01:59:45 +0800434
Patrick Williams5d82f472022-07-22 19:26:53 -0500435 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Yong Li18d77262018-10-09 01:59:45 +0800436
Yong Li18d77262018-10-09 01:59:45 +0800437 try
438 {
439 auto acpiObject = ipmi::getDbusObject(bus, acpi_state::acpiInterface);
440
441 auto sysACPIVal = ipmi::getDbusProperty(
442 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
443 acpi_state::sysACPIProp);
444 auto sysACPI = acpi_state::ACPIPowerState::convertACPIFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -0700445 std::get<std::string>(sysACPIVal));
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000446 sysAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(sysACPI));
Yong Li18d77262018-10-09 01:59:45 +0800447
448 auto devACPIVal = ipmi::getDbusProperty(
449 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
450 acpi_state::devACPIProp);
451 auto devACPI = acpi_state::ACPIPowerState::convertACPIFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -0700452 std::get<std::string>(devACPIVal));
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000453 devAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(devACPI));
Yong Li18d77262018-10-09 01:59:45 +0800454 }
455 catch (const InternalFailure& e)
456 {
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000457 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800458 }
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000459
460 return ipmi::responseSuccess(sysAcpiState, devAcpiState);
Chris Austen6caf28b2015-10-13 12:40:40 -0500461}
462
Chris Austen7303bdc2016-04-17 11:50:54 -0500463typedef struct
464{
465 char major;
466 char minor;
Potin Laic7c55922023-02-16 10:33:55 +0800467 uint8_t aux[4];
Vernon Mauery86a50822019-03-25 13:11:36 -0700468} Revision;
Chris Austen7303bdc2016-04-17 11:50:54 -0500469
Potin Lai5d214202023-01-16 18:11:49 +0800470/* Use regular expression searching matched pattern X.Y, and convert it to */
471/* Major (X) and Minor (Y) version. */
472/* Example: */
473/* version = 2.14.0-dev */
474/* ^ ^ */
475/* | |---------------- Minor */
476/* |------------------ Major */
477/* */
Potin Laic7c55922023-02-16 10:33:55 +0800478/* Default regex string only tries to match Major and Minor version. */
479/* */
480/* To match more firmware version info, platforms need to define it own */
481/* regex string to match more strings, and assign correct mapping index in */
482/* matches array. */
483/* */
484/* matches[0]: matched index for major ver */
485/* matches[1]: matched index for minor ver */
486/* matches[2]: matched index for aux[0] (set 0 to skip) */
487/* matches[3]: matched index for aux[1] (set 0 to skip) */
488/* matches[4]: matched index for aux[2] (set 0 to skip) */
489/* matches[5]: matched index for aux[3] (set 0 to skip) */
490/* Example: */
491/* regex = "([\d]+).([\d]+).([\d]+)-dev-([\d]+)-g([0-9a-fA-F]{2}) */
492/* ([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})" */
493/* matches = {1,2,5,6,7,8} */
494/* version = 2.14.0-dev-750-g37a7c5ad1-dirty */
495/* ^ ^ ^ ^ ^ ^ ^ ^ */
496/* | | | | | | | | */
497/* | | | | | | | |-- Aux byte 3 (0xAD), index 8 */
498/* | | | | | | |---- Aux byte 2 (0xC5), index 7 */
499/* | | | | | |------ Aux byte 1 (0xA7), index 6 */
500/* | | | | |-------- Aux byte 0 (0x37), index 5 */
501/* | | | |------------- Not used, index 4 */
502/* | | |------------------- Not used, index 3 */
503/* | |---------------------- Minor (14), index 2 */
504/* |------------------------ Major (2), index 1 */
Potin Lai5d214202023-01-16 18:11:49 +0800505int convertVersion(std::string s, Revision& rev)
Chris Austen7303bdc2016-04-17 11:50:54 -0500506{
Potin Laic7c55922023-02-16 10:33:55 +0800507 static const std::vector<size_t> matches = {
508 MAJOR_MATCH_INDEX, MINOR_MATCH_INDEX, AUX_0_MATCH_INDEX,
509 AUX_1_MATCH_INDEX, AUX_2_MATCH_INDEX, AUX_3_MATCH_INDEX};
Potin Lai5d214202023-01-16 18:11:49 +0800510 std::regex fw_regex(FW_VER_REGEX);
511 std::smatch m;
512 Revision r = {0};
513 size_t val;
Chris Austen7303bdc2016-04-17 11:50:54 -0500514
Potin Laidcde04e2023-03-16 18:44:15 +0800515 if (std::regex_search(s, m, fw_regex))
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600516 {
Potin Laic7c55922023-02-16 10:33:55 +0800517 if (m.size() < *std::max_element(matches.begin(), matches.end()))
518 { // max index higher than match count
Potin Laidcde04e2023-03-16 18:44:15 +0800519 return -1;
520 }
521
Potin Lai5d214202023-01-16 18:11:49 +0800522 // convert major
523 {
Potin Laic7c55922023-02-16 10:33:55 +0800524 std::string_view str = m[matches[0]].str();
Potin Lai5d214202023-01-16 18:11:49 +0800525 auto [ptr, ec]{std::from_chars(str.begin(), str.end(), val)};
526 if (ec != std::errc() || ptr != str.begin() + str.size())
527 { // failed to convert major string
Potin Laidcde04e2023-03-16 18:44:15 +0800528 return -1;
Potin Lai5d214202023-01-16 18:11:49 +0800529 }
Potin Lai058e2912023-09-07 10:16:38 +0800530
531 if (val >= 2000)
532 { // For the platforms use year as major version, it would expect to
533 // have major version between 0 - 99. If the major version is
534 // greater than or equal to 2000, it is treated as a year and
535 // converted to 0 - 99.
536 r.major = val % 100;
537 }
538 else
539 {
540 r.major = val & 0x7F;
541 }
Potin Lai5d214202023-01-16 18:11:49 +0800542 }
543
544 // convert minor
545 {
Potin Laic7c55922023-02-16 10:33:55 +0800546 std::string_view str = m[matches[1]].str();
Potin Lai5d214202023-01-16 18:11:49 +0800547 auto [ptr, ec]{std::from_chars(str.begin(), str.end(), val)};
548 if (ec != std::errc() || ptr != str.begin() + str.size())
549 { // failed to convert minor string
Potin Laidcde04e2023-03-16 18:44:15 +0800550 return -1;
Potin Lai5d214202023-01-16 18:11:49 +0800551 }
552 r.minor = val & 0xFF;
553 }
554
Potin Laic7c55922023-02-16 10:33:55 +0800555 // convert aux bytes
556 {
557 size_t i;
558 for (i = 0; i < 4; i++)
559 {
560 if (matches[i + 2] == 0)
561 {
562 continue;
563 }
564
565 std::string_view str = m[matches[i + 2]].str();
566 auto [ptr,
567 ec]{std::from_chars(str.begin(), str.end(), val, 16)};
568 if (ec != std::errc() || ptr != str.begin() + str.size())
569 { // failed to convert aux byte string
570 break;
571 }
572
573 r.aux[i] = val & 0xFF;
574 }
575
576 if (i != 4)
577 { // something wrong durign converting aux bytes
578 return -1;
579 }
580 }
581
Potin Lai5d214202023-01-16 18:11:49 +0800582 // all matched
583 rev = r;
584 return 0;
Chris Austen176c9652016-04-30 16:32:17 -0500585 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500586
Potin Lai5d214202023-01-16 18:11:49 +0800587 return -1;
Chris Austen7303bdc2016-04-17 11:50:54 -0500588}
589
Vernon Maueryea1c4012019-05-24 13:26:16 -0700590/* @brief: Implement the Get Device ID IPMI command per the IPMI spec
591 * @param[in] ctx - shared_ptr to an IPMI context struct
592 *
593 * @returns IPMI completion code plus response data
594 * - Device ID (manufacturer defined)
595 * - Device revision[4 bits]; reserved[3 bits]; SDR support[1 bit]
596 * - FW revision major[7 bits] (binary encoded); available[1 bit]
597 * - FW Revision minor (BCD encoded)
598 * - IPMI version (0x02 for IPMI 2.0)
599 * - device support (bitfield of supported options)
600 * - MFG IANA ID (3 bytes)
601 * - product ID (2 bytes)
602 * - AUX info (4 bytes)
603 */
604ipmi::RspType<uint8_t, // Device ID
605 uint8_t, // Device Revision
606 uint8_t, // Firmware Revision Major
607 uint8_t, // Firmware Revision minor
608 uint8_t, // IPMI version
609 uint8_t, // Additional device support
610 uint24_t, // MFG ID
611 uint16_t, // Product ID
612 uint32_t // AUX info
613 >
Willy Tu11d68892022-01-20 10:37:34 -0800614 ipmiAppGetDeviceId([[maybe_unused]] ipmi::Context::ptr ctx)
Chris Austen6caf28b2015-10-13 12:40:40 -0500615{
Vernon Mauery86a50822019-03-25 13:11:36 -0700616 static struct
617 {
618 uint8_t id;
619 uint8_t revision;
620 uint8_t fw[2];
621 uint8_t ipmiVer;
622 uint8_t addnDevSupport;
623 uint24_t manufId;
624 uint16_t prodId;
625 uint32_t aux;
626 } devId;
David Cobbleya1adb072017-11-21 15:58:13 -0800627 static bool dev_id_initialized = false;
Patrick Venture94930a12019-04-30 10:01:58 -0700628 static bool defaultActivationSetting = true;
David Cobbleya1adb072017-11-21 15:58:13 -0800629 const char* filename = "/usr/share/ipmi-providers/dev_id.json";
Alexander Amelkinba19c182018-09-04 15:49:36 +0300630 constexpr auto ipmiDevIdStateShift = 7;
631 constexpr auto ipmiDevIdFw1Mask = ~(1 << ipmiDevIdStateShift);
Willy Tub78184e2022-10-27 22:57:38 +0000632
633#ifdef GET_DBUS_ACTIVE_SOFTWARE
634 static bool haveBMCVersion = false;
JeffLin27a62ec2021-11-24 15:40:33 +0800635 if (!haveBMCVersion || !dev_id_initialized)
David Cobbleya1adb072017-11-21 15:58:13 -0800636 {
Willy Tub78184e2022-10-27 22:57:38 +0000637 int r = -1;
638 Revision rev = {0, 0, 0, 0};
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600639 try
640 {
Vernon Maueryea1c4012019-05-24 13:26:16 -0700641 auto version = getActiveSoftwareVersionInfo(ctx);
Vernon Mauery86a50822019-03-25 13:11:36 -0700642 r = convertVersion(version, rev);
David Cobbleya1adb072017-11-21 15:58:13 -0800643 }
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600644 catch (const std::exception& e)
645 {
George Liu24fffdc2024-07-17 17:40:53 +0800646 lg2::error("error message: {ERROR}", "ERROR", e);
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600647 }
Nan Liee0cb902016-07-11 15:38:03 +0800648
Patrick Venture0b02be92018-08-31 11:55:55 -0700649 if (r >= 0)
650 {
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600651 // bit7 identifies if the device is available
652 // 0=normal operation
653 // 1=device firmware, SDR update,
654 // or self-initialization in progress.
Alexander Amelkinba19c182018-09-04 15:49:36 +0300655 // The availability may change in run time, so mask here
656 // and initialize later.
Vernon Mauery86a50822019-03-25 13:11:36 -0700657 devId.fw[0] = rev.major & ipmiDevIdFw1Mask;
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600658
659 rev.minor = (rev.minor > 99 ? 99 : rev.minor);
Vernon Mauery86a50822019-03-25 13:11:36 -0700660 devId.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16;
Potin Laic7c55922023-02-16 10:33:55 +0800661 std::memcpy(&devId.aux, rev.aux, sizeof(rev.aux));
Brandon Kimc1f5aca2022-03-17 17:54:06 -0700662 haveBMCVersion = true;
David Cobbleya1adb072017-11-21 15:58:13 -0800663 }
JeffLin27a62ec2021-11-24 15:40:33 +0800664 }
Willy Tub78184e2022-10-27 22:57:38 +0000665#endif
JeffLin27a62ec2021-11-24 15:40:33 +0800666 if (!dev_id_initialized)
667 {
David Cobbleya1adb072017-11-21 15:58:13 -0800668 // IPMI Spec version 2.0
Vernon Mauery86a50822019-03-25 13:11:36 -0700669 devId.ipmiVer = 2;
Adriana Kobylak0e912642016-06-22 16:54:39 -0500670
Vernon Mauery86a50822019-03-25 13:11:36 -0700671 std::ifstream devIdFile(filename);
672 if (devIdFile.is_open())
David Cobbleya1adb072017-11-21 15:58:13 -0800673 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700674 auto data = nlohmann::json::parse(devIdFile, nullptr, false);
David Cobbleya1adb072017-11-21 15:58:13 -0800675 if (!data.is_discarded())
676 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700677 devId.id = data.value("id", 0);
678 devId.revision = data.value("revision", 0);
679 devId.addnDevSupport = data.value("addn_dev_support", 0);
680 devId.manufId = data.value("manuf_id", 0);
681 devId.prodId = data.value("prod_id", 0);
Potin Lai9a686362023-09-05 09:27:53 +0800682#ifdef GET_DBUS_ACTIVE_SOFTWARE
683 if (!(AUX_0_MATCH_INDEX || AUX_1_MATCH_INDEX ||
684 AUX_2_MATCH_INDEX || AUX_3_MATCH_INDEX))
685#endif
686 {
687 devId.aux = data.value("aux", 0);
688 }
David Cobbleya1adb072017-11-21 15:58:13 -0800689
Willy Tubfd3a172022-05-31 13:57:54 -0700690 if (data.contains("firmware_revision"))
691 {
692 const auto& firmwareRevision = data.at("firmware_revision");
693 if (firmwareRevision.contains("major"))
694 {
695 firmwareRevision.at("major").get_to(devId.fw[0]);
696 }
697 if (firmwareRevision.contains("minor"))
698 {
699 firmwareRevision.at("minor").get_to(devId.fw[1]);
700 }
701 }
702
Patrick Venture94930a12019-04-30 10:01:58 -0700703 // Set the availablitity of the BMC.
704 defaultActivationSetting = data.value("availability", true);
705
Patrick Venture0b02be92018-08-31 11:55:55 -0700706 // Don't read the file every time if successful
David Cobbleya1adb072017-11-21 15:58:13 -0800707 dev_id_initialized = true;
708 }
709 else
710 {
George Liu24fffdc2024-07-17 17:40:53 +0800711 lg2::error("Device ID JSON parser failure");
Vernon Maueryf2587fc2019-04-09 14:28:15 -0700712 return ipmi::responseUnspecifiedError();
David Cobbleya1adb072017-11-21 15:58:13 -0800713 }
714 }
715 else
716 {
George Liu24fffdc2024-07-17 17:40:53 +0800717 lg2::error("Device ID file not found");
Vernon Maueryf2587fc2019-04-09 14:28:15 -0700718 return ipmi::responseUnspecifiedError();
Chris Austen7303bdc2016-04-17 11:50:54 -0500719 }
720 }
Chris Austen6caf28b2015-10-13 12:40:40 -0500721
Alexander Amelkinba19c182018-09-04 15:49:36 +0300722 // Set availability to the actual current BMC state
Vernon Mauery86a50822019-03-25 13:11:36 -0700723 devId.fw[0] &= ipmiDevIdFw1Mask;
Patrick Venture94930a12019-04-30 10:01:58 -0700724 if (!getCurrentBmcStateWithFallback(defaultActivationSetting))
Alexander Amelkinba19c182018-09-04 15:49:36 +0300725 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700726 devId.fw[0] |= (1 << ipmiDevIdStateShift);
Alexander Amelkinba19c182018-09-04 15:49:36 +0300727 }
728
Vernon Mauery86a50822019-03-25 13:11:36 -0700729 return ipmi::responseSuccess(
730 devId.id, devId.revision, devId.fw[0], devId.fw[1], devId.ipmiVer,
731 devId.addnDevSupport, devId.manufId, devId.prodId, devId.aux);
Chris Austen6caf28b2015-10-13 12:40:40 -0500732}
733
Vernon Maueryb84a5282019-03-25 13:39:03 -0700734auto ipmiAppGetSelfTestResults() -> ipmi::RspType<uint8_t, uint8_t>
Nan Li41fa24a2016-11-10 20:12:37 +0800735{
Nan Li41fa24a2016-11-10 20:12:37 +0800736 // Byte 2:
737 // 55h - No error.
Gunnar Mills8991dd62017-10-25 17:11:29 -0500738 // 56h - Self Test function not implemented in this controller.
Nan Li41fa24a2016-11-10 20:12:37 +0800739 // 57h - Corrupted or inaccesssible data or devices.
740 // 58h - Fatal hardware error.
741 // FFh - reserved.
742 // all other: Device-specific 'internal failure'.
743 // Byte 3:
744 // For byte 2 = 55h, 56h, FFh: 00h
745 // For byte 2 = 58h, all other: Device-specific
746 // For byte 2 = 57h: self-test error bitfield.
747 // Note: returning 57h does not imply that all test were run.
748 // [7] 1b = Cannot access SEL device.
749 // [6] 1b = Cannot access SDR Repository.
750 // [5] 1b = Cannot access BMC FRU device.
751 // [4] 1b = IPMB signal lines do not respond.
752 // [3] 1b = SDR Repository empty.
753 // [2] 1b = Internal Use Area of BMC FRU corrupted.
754 // [1] 1b = controller update 'boot block' firmware corrupted.
755 // [0] 1b = controller operational firmware corrupted.
Vernon Maueryb84a5282019-03-25 13:39:03 -0700756 constexpr uint8_t notImplemented = 0x56;
757 constexpr uint8_t zero = 0;
758 return ipmi::responseSuccess(notImplemented, zero);
Nan Li41fa24a2016-11-10 20:12:37 +0800759}
760
Vernon Mauery15541322019-03-25 13:33:03 -0700761static constexpr size_t uuidBinaryLength = 16;
762static std::array<uint8_t, uuidBinaryLength> rfc4122ToIpmi(std::string rfc4122)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500763{
Willy Tu523e2d12023-09-05 11:36:48 -0700764 using Argument = xyz::openbmc_project::common::InvalidArgument;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500765 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
Patrick Ventured2117022018-02-06 08:54:37 -0800766 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte
767 // order
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500768 // Ex: 0x2332fc2c40e66298e511f2782395a361
Vernon Mauery15541322019-03-25 13:33:03 -0700769 constexpr size_t uuidHexLength = (2 * uuidBinaryLength);
770 constexpr size_t uuidRfc4122Length = (uuidHexLength + 4);
771 std::array<uint8_t, uuidBinaryLength> uuid;
772 if (rfc4122.size() == uuidRfc4122Length)
Patrick Venture0b02be92018-08-31 11:55:55 -0700773 {
Vernon Mauery15541322019-03-25 13:33:03 -0700774 rfc4122.erase(std::remove(rfc4122.begin(), rfc4122.end(), '-'),
775 rfc4122.end());
Sergey Solomineb9b8142016-08-23 09:07:28 -0500776 }
Vernon Mauery15541322019-03-25 13:33:03 -0700777 if (rfc4122.size() != uuidHexLength)
vishwa1eaea4f2016-02-26 11:57:40 -0600778 {
Vernon Mauery15541322019-03-25 13:33:03 -0700779 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
780 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
vishwa1eaea4f2016-02-26 11:57:40 -0600781 }
Vernon Mauery15541322019-03-25 13:33:03 -0700782 for (size_t ind = 0; ind < uuidHexLength; ind += 2)
vishwa1eaea4f2016-02-26 11:57:40 -0600783 {
Vernon Mauery15541322019-03-25 13:33:03 -0700784 char v[3];
785 v[0] = rfc4122[ind];
786 v[1] = rfc4122[ind + 1];
787 v[2] = 0;
788 size_t err;
789 long b;
790 try
Emily Shafferedb8bb02018-09-27 14:50:15 -0700791 {
Vernon Mauery15541322019-03-25 13:33:03 -0700792 b = std::stoul(v, &err, 16);
Emily Shafferedb8bb02018-09-27 14:50:15 -0700793 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500794 catch (const std::exception& e)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500795 {
Vernon Mauery15541322019-03-25 13:33:03 -0700796 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
797 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500798 }
Vernon Mauery15541322019-03-25 13:33:03 -0700799 // check that exactly two ascii bytes were converted
800 if (err != 2)
801 {
802 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
803 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
804 }
805 uuid[uuidBinaryLength - (ind / 2) - 1] = static_cast<uint8_t>(b);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500806 }
Vernon Mauery15541322019-03-25 13:33:03 -0700807 return uuid;
808}
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500809
Vernon Mauery15541322019-03-25 13:33:03 -0700810auto ipmiAppGetDeviceGuid()
811 -> ipmi::RspType<std::array<uint8_t, uuidBinaryLength>>
812{
813 // return a fixed GUID based on /etc/machine-id
814 // This should match the /redfish/v1/Managers/bmc's UUID data
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500815
Vernon Mauery15541322019-03-25 13:33:03 -0700816 // machine specific application ID (for BMC ID)
817 // generated by systemd-id128 -p new as per man page
818 static constexpr sd_id128_t bmcUuidAppId = SD_ID128_MAKE(
819 e0, e1, 73, 76, 64, 61, 47, da, a5, 0c, d0, cc, 64, 12, 45, 78);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500820
Vernon Mauery15541322019-03-25 13:33:03 -0700821 sd_id128_t bmcUuid;
822 // create the UUID from /etc/machine-id via the systemd API
823 sd_id128_get_machine_app_specific(bmcUuidAppId, &bmcUuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500824
Vernon Mauery15541322019-03-25 13:33:03 -0700825 char bmcUuidCstr[SD_ID128_STRING_MAX];
826 std::string systemUuid = sd_id128_to_string(bmcUuid, bmcUuidCstr);
827
828 std::array<uint8_t, uuidBinaryLength> uuid = rfc4122ToIpmi(systemUuid);
829 return ipmi::responseSuccess(uuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500830}
Chris Austen6caf28b2015-10-13 12:40:40 -0500831
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700832auto ipmiAppGetBtCapabilities()
833 -> ipmi::RspType<uint8_t, uint8_t, uint8_t, uint8_t, uint8_t>
vishwabmcba0bd5f2015-09-30 16:50:23 +0530834{
Adriana Kobylak88ad8152016-12-13 10:09:08 -0600835 // Per IPMI 2.0 spec, the input and output buffer size must be the max
836 // buffer size minus one byte to allocate space for the length byte.
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700837 constexpr uint8_t nrOutstanding = 0x01;
838 constexpr uint8_t inputBufferSize = MAX_IPMI_BUFFER - 1;
839 constexpr uint8_t outputBufferSize = MAX_IPMI_BUFFER - 1;
840 constexpr uint8_t transactionTime = 0x0A;
841 constexpr uint8_t nrRetries = 0x01;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530842
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700843 return ipmi::responseSuccess(nrOutstanding, inputBufferSize,
844 outputBufferSize, transactionTime, nrRetries);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530845}
846
Vernon Mauery6c44a942023-07-27 11:00:10 -0700847auto ipmiAppGetSystemGuid(ipmi::Context::ptr& ctx)
848 -> ipmi::RspType<std::array<uint8_t, 16>>
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600849{
Vernon Maueryb90a5322019-03-25 13:36:55 -0700850 static constexpr auto uuidInterface = "xyz.openbmc_project.Common.UUID";
851 static constexpr auto uuidProperty = "UUID";
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600852
Vernon Mauery6c44a942023-07-27 11:00:10 -0700853 // Get the Inventory object implementing BMC interface
854 ipmi::DbusObjectInfo objectInfo{};
855 boost::system::error_code ec = ipmi::getDbusObject(ctx, uuidInterface,
856 objectInfo);
857 if (ec.value())
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600858 {
George Liu24fffdc2024-07-17 17:40:53 +0800859 lg2::error("Failed to locate System UUID object, "
860 "interface: {INTERFACE}, error: {ERROR}",
861 "INTERFACE", uuidInterface, "ERROR", ec.message());
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600862 }
Vernon Mauery6c44a942023-07-27 11:00:10 -0700863
864 // Read UUID property value from bmcObject
865 // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
866 std::string rfc4122Uuid{};
867 ec = ipmi::getDbusProperty(ctx, objectInfo.second, objectInfo.first,
868 uuidInterface, uuidProperty, rfc4122Uuid);
869 if (ec.value())
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600870 {
George Liu24fffdc2024-07-17 17:40:53 +0800871 lg2::error("Failed to read System UUID property, "
872 "interface: {INTERFACE}, property: {PROPERTY}, "
873 "error: {ERROR}",
874 "INTERFACE", uuidInterface, "PROPERTY", uuidProperty,
875 "ERROR", ec.message());
Vernon Maueryb90a5322019-03-25 13:36:55 -0700876 return ipmi::responseUnspecifiedError();
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600877 }
Vernon Maueryb90a5322019-03-25 13:36:55 -0700878 std::array<uint8_t, 16> uuid;
Vernon Maueryb90a5322019-03-25 13:36:55 -0700879 try
880 {
881 // convert to IPMI format
882 uuid = rfc4122ToIpmi(rfc4122Uuid);
883 }
884 catch (const InvalidArgument& e)
885 {
George Liu24fffdc2024-07-17 17:40:53 +0800886 lg2::error("Failed in parsing BMC UUID property, "
887 "interface: {INTERFACE}, property: {PROPERTY}, "
888 "value: {VALUE}, error: {ERROR}",
889 "INTERFACE", uuidInterface, "PROPERTY", uuidProperty,
890 "VALUE", rfc4122Uuid, "ERROR", e);
Vernon Maueryb90a5322019-03-25 13:36:55 -0700891 return ipmi::responseUnspecifiedError();
892 }
893 return ipmi::responseSuccess(uuid);
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600894}
895
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000896/**
897 * @brief set the session state as teardown
898 *
899 * This function is to set the session state to tear down in progress if the
900 * state is active.
901 *
902 * @param[in] busp - Dbus obj
903 * @param[in] service - service name
904 * @param[in] obj - object path
905 *
906 * @return success completion code if it sets the session state to
907 * tearDownInProgress else return the corresponding error completion code.
908 **/
909uint8_t setSessionState(std::shared_ptr<sdbusplus::asio::connection>& busp,
910 const std::string& service, const std::string& obj)
911{
912 try
913 {
914 uint8_t sessionState = std::get<uint8_t>(ipmi::getDbusProperty(
915 *busp, service, obj, session::sessionIntf, "State"));
916
917 if (sessionState == static_cast<uint8_t>(session::State::active))
918 {
919 ipmi::setDbusProperty(
920 *busp, service, obj, session::sessionIntf, "State",
921 static_cast<uint8_t>(session::State::tearDownInProgress));
922 return ipmi::ccSuccess;
923 }
924 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500925 catch (const std::exception& e)
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000926 {
George Liu24fffdc2024-07-17 17:40:53 +0800927 lg2::error("Failed in getting session state property, "
928 "service: {SERVICE}, object path: {OBJECT_PATH}, "
929 "interface: {INTERFACE}, error: {ERROR}",
930 "SERVICE", service, "OBJECT_PATH", obj, "INTERFACE",
931 session::sessionIntf, "ERROR", e);
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000932 return ipmi::ccUnspecifiedError;
933 }
934
935 return ipmi::ccInvalidFieldRequest;
936}
937
938ipmi::RspType<> ipmiAppCloseSession(uint32_t reqSessionId,
939 std::optional<uint8_t> requestSessionHandle)
940{
941 auto busp = getSdBus();
942 uint8_t reqSessionHandle =
943 requestSessionHandle.value_or(session::defaultSessionHandle);
944
945 if (reqSessionId == session::sessionZero &&
946 reqSessionHandle == session::defaultSessionHandle)
947 {
948 return ipmi::response(session::ccInvalidSessionId);
949 }
950
951 if (reqSessionId == session::sessionZero &&
952 reqSessionHandle == session::invalidSessionHandle)
953 {
954 return ipmi::response(session::ccInvalidSessionHandle);
955 }
956
957 if (reqSessionId != session::sessionZero &&
958 reqSessionHandle != session::defaultSessionHandle)
959 {
960 return ipmi::response(ipmi::ccInvalidFieldRequest);
961 }
962
963 try
964 {
965 ipmi::ObjectTree objectTree = ipmi::getAllDbusObjects(
966 *busp, session::sessionManagerRootPath, session::sessionIntf);
967
968 for (auto& objectTreeItr : objectTree)
969 {
970 const std::string obj = objectTreeItr.first;
971
972 if (isSessionObjectMatched(obj, reqSessionId, reqSessionHandle))
973 {
974 auto& serviceMap = objectTreeItr.second;
975
976 // Session id and session handle are unique for each session.
977 // Session id and handler are retrived from the object path and
978 // object path will be unique for each session. Checking if
979 // multiple objects exist with same object path under multiple
980 // services.
981 if (serviceMap.size() != 1)
982 {
983 return ipmi::responseUnspecifiedError();
984 }
985
986 auto itr = serviceMap.begin();
987 const std::string service = itr->first;
988 return ipmi::response(setSessionState(busp, service, obj));
989 }
990 }
991 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500992 catch (const sdbusplus::exception_t& e)
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000993 {
George Liu24fffdc2024-07-17 17:40:53 +0800994 lg2::error("Failed to fetch object from dbus, "
995 "interface: {INTERFACE}, error: {ERROR}",
996 "INTERFACE", session::sessionIntf, "ERROR", e);
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000997 return ipmi::responseUnspecifiedError();
998 }
999
1000 return ipmi::responseInvalidFieldRequest();
1001}
1002
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001003uint8_t getTotalSessionCount()
1004{
Meera-Kattac1789482021-05-18 09:53:26 +00001005 uint8_t count = 0, ch = 0;
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001006
1007 while (ch < ipmi::maxIpmiChannels &&
1008 count < session::maxNetworkInstanceSupported)
1009 {
Jayaprakash Mutyala80207e62020-07-04 16:34:15 +00001010 ipmi::ChannelInfo chInfo{};
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001011 ipmi::getChannelInfo(ch, chInfo);
1012 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) ==
1013 ipmi::EChannelMediumType::lan8032)
1014 {
1015 count++;
1016 }
1017 ch++;
1018 }
1019 return count * session::maxSessionCountPerChannel;
1020}
1021
1022/**
1023 * @brief get session info request data.
1024 *
1025 * This function validates the request data and retrive request session id,
1026 * session handle.
1027 *
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301028 * @param[in] ctx - context of current session.
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001029 * @param[in] sessionIndex - request session index
1030 * @param[in] payload - input payload
1031 * @param[in] reqSessionId - unpacked session Id will be asigned
1032 * @param[in] reqSessionHandle - unpacked session handle will be asigned
1033 *
1034 * @return success completion code if request data is valid
1035 * else return the correcponding error completion code.
1036 **/
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301037uint8_t getSessionInfoRequestData(const ipmi::Context::ptr ctx,
1038 const uint8_t sessionIndex,
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001039 ipmi::message::Payload& payload,
1040 uint32_t& reqSessionId,
1041 uint8_t& reqSessionHandle)
1042{
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301043 if ((sessionIndex > session::maxSessionCountPerChannel) &&
1044 (sessionIndex < session::searchSessionByHandle))
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001045 {
1046 return ipmi::ccInvalidFieldRequest;
1047 }
1048
1049 switch (sessionIndex)
1050 {
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301051 case session::searchCurrentSession:
1052
1053 ipmi::ChannelInfo chInfo;
1054 ipmi::getChannelInfo(ctx->channel, chInfo);
1055
1056 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) !=
1057 ipmi::EChannelMediumType::lan8032)
1058 {
1059 return ipmi::ccInvalidFieldRequest;
1060 }
1061
1062 if (!payload.fullyUnpacked())
1063 {
1064 return ipmi::ccReqDataLenInvalid;
1065 }
1066 // Check if current sessionId is 0, sessionId 0 is reserved.
1067 if (ctx->sessionId == session::sessionZero)
1068 {
1069 return session::ccInvalidSessionId;
1070 }
1071 reqSessionId = ctx->sessionId;
1072 break;
1073
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001074 case session::searchSessionByHandle:
1075
1076 if ((payload.unpack(reqSessionHandle)) ||
1077 (!payload.fullyUnpacked()))
1078 {
1079 return ipmi::ccReqDataLenInvalid;
1080 }
1081
1082 if ((reqSessionHandle == session::sessionZero) ||
1083 ((reqSessionHandle & session::multiIntfaceSessionHandleMask) >
1084 session::maxSessionCountPerChannel))
1085 {
1086 return session::ccInvalidSessionHandle;
1087 }
1088 break;
1089
1090 case session::searchSessionById:
1091
1092 if ((payload.unpack(reqSessionId)) || (!payload.fullyUnpacked()))
1093 {
1094 return ipmi::ccReqDataLenInvalid;
1095 }
1096
1097 if (reqSessionId == session::sessionZero)
1098 {
1099 return session::ccInvalidSessionId;
1100 }
1101 break;
1102
1103 default:
1104 if (!payload.fullyUnpacked())
1105 {
1106 return ipmi::ccReqDataLenInvalid;
1107 }
1108 break;
1109 }
1110 return ipmi::ccSuccess;
1111}
1112
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001113uint8_t getSessionState(ipmi::Context::ptr ctx, const std::string& service,
1114 const std::string& objPath, uint8_t& sessionState)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001115{
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001116 boost::system::error_code ec = ipmi::getDbusProperty(
1117 ctx, service, objPath, session::sessionIntf, "State", sessionState);
1118 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001119 {
George Liu24fffdc2024-07-17 17:40:53 +08001120 lg2::error("Failed to fetch state property, service: {SERVICE}, "
1121 "object path: {OBJECTPATH}, interface: {INTERFACE}, "
1122 "error: {ERROR}",
1123 "SERVICE", service, "OBJECTPATH", objPath, "INTERFACE",
1124 session::sessionIntf, "ERROR", ec.message());
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001125 return ipmi::ccUnspecifiedError;
1126 }
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001127 return ipmi::ccSuccess;
1128}
1129
1130static constexpr uint8_t macAddrLen = 6;
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001131/** Alias SessionDetails - contain the optional information about an
1132 * RMCP+ session.
1133 *
1134 * @param userID - uint6_t session user ID (0-63)
1135 * @param reserved - uint2_t reserved
1136 * @param privilege - uint4_t session privilege (0-5)
1137 * @param reserved - uint4_t reserved
1138 * @param channel - uint4_t session channel number
1139 * @param protocol - uint4_t session protocol
1140 * @param remoteIP - uint32_t remote IP address
1141 * @param macAddr - std::array<uint8_t, 6> mac address
1142 * @param port - uint16_t remote port
1143 */
1144using SessionDetails =
1145 std::tuple<uint2_t, uint6_t, uint4_t, uint4_t, uint4_t, uint4_t, uint32_t,
1146 std::array<uint8_t, macAddrLen>, uint16_t>;
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001147
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001148/** @brief get session details for a given session
1149 *
1150 * @param[in] ctx - ipmi::Context pointer for accessing D-Bus
1151 * @param[in] service - D-Bus service name to fetch details from
1152 * @param[in] objPath - D-Bus object path for session
1153 * @param[out] sessionHandle - return session handle for session
1154 * @param[out] sessionState - return session state for session
1155 * @param[out] details - return a SessionDetails tuple containing other
1156 * session info
1157 * @return - ipmi::Cc success or error code
1158 */
1159ipmi::Cc getSessionDetails(ipmi::Context::ptr ctx, const std::string& service,
1160 const std::string& objPath, uint8_t& sessionHandle,
1161 uint8_t& sessionState, SessionDetails& details)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001162{
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001163 ipmi::PropertyMap sessionProps;
1164 boost::system::error_code ec = ipmi::getAllDbusProperties(
1165 ctx, service, objPath, session::sessionIntf, sessionProps);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001166
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001167 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001168 {
George Liu24fffdc2024-07-17 17:40:53 +08001169 lg2::error("Failed to fetch state property, service: {SERVICE}, "
1170 "object path: {OBJECTPATH}, interface: {INTERFACE}, "
1171 "error: {ERROR}",
1172 "SERVICE", service, "OBJECTPATH", objPath, "INTERFACE",
1173 session::sessionIntf, "ERROR", ec.message());
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001174 return ipmi::ccUnspecifiedError;
1175 }
1176
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001177 sessionState = ipmi::mappedVariant<uint8_t>(
1178 sessionProps, "State", static_cast<uint8_t>(session::State::inactive));
1179 if (sessionState == static_cast<uint8_t>(session::State::active))
1180 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001181 sessionHandle = ipmi::mappedVariant<uint8_t>(sessionProps,
1182 "SessionHandle", 0);
1183 std::get<0>(details) = ipmi::mappedVariant<uint8_t>(sessionProps,
1184 "UserID", 0xff);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001185 // std::get<1>(details) = 0; // (default constructed to 0)
1186 std::get<2>(details) =
1187 ipmi::mappedVariant<uint8_t>(sessionProps, "CurrentPrivilege", 0);
1188 // std::get<3>(details) = 0; // (default constructed to 0)
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001189 std::get<4>(details) = ipmi::mappedVariant<uint8_t>(sessionProps,
1190 "ChannelNum", 0xff);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001191 constexpr uint4_t rmcpPlusProtocol = 1;
1192 std::get<5>(details) = rmcpPlusProtocol;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001193 std::get<6>(details) = ipmi::mappedVariant<uint32_t>(sessionProps,
1194 "RemoteIPAddr", 0);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001195 // std::get<7>(details) = {{0}}; // default constructed to all 0
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001196 std::get<8>(details) = ipmi::mappedVariant<uint16_t>(sessionProps,
1197 "RemotePort", 0);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001198 }
1199
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001200 return ipmi::ccSuccess;
1201}
1202
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001203ipmi::RspType<uint8_t, // session handle,
1204 uint8_t, // total session count
1205 uint8_t, // active session count
1206 std::optional<SessionDetails>>
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301207 ipmiAppGetSessionInfo(ipmi::Context::ptr ctx, uint8_t sessionIndex,
1208 ipmi::message::Payload& payload)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001209{
1210 uint32_t reqSessionId = 0;
1211 uint8_t reqSessionHandle = session::defaultSessionHandle;
1212 // initializing state to 0xff as 0 represents state as inactive.
1213 uint8_t state = 0xFF;
1214
1215 uint8_t completionCode = getSessionInfoRequestData(
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301216 ctx, sessionIndex, payload, reqSessionId, reqSessionHandle);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001217
1218 if (completionCode)
1219 {
1220 return ipmi::response(completionCode);
1221 }
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001222 ipmi::ObjectTree objectTree;
1223 boost::system::error_code ec = ipmi::getAllDbusObjects(
1224 ctx, session::sessionManagerRootPath, session::sessionIntf, objectTree);
1225 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001226 {
George Liu24fffdc2024-07-17 17:40:53 +08001227 lg2::error("Failed to fetch object from dbus, "
1228 "interface: {INTERFACE}, error: {ERROR}",
1229 "INTERFACE", session::sessionIntf, "ERROR", ec.message());
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001230 return ipmi::responseUnspecifiedError();
1231 }
1232
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001233 uint8_t totalSessionCount = getTotalSessionCount();
1234 uint8_t activeSessionCount = 0;
1235 uint8_t sessionHandle = session::defaultSessionHandle;
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001236 uint8_t activeSessionHandle = 0;
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001237 std::optional<SessionDetails> maybeDetails;
1238 uint8_t index = 0;
1239 for (auto& objectTreeItr : objectTree)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001240 {
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001241 uint32_t sessionId = 0;
1242 std::string objectPath = objectTreeItr.first;
1243
1244 if (!parseCloseSessionInputPayload(objectPath, sessionId,
1245 sessionHandle))
1246 {
1247 continue;
1248 }
1249 index++;
1250 auto& serviceMap = objectTreeItr.second;
1251 auto itr = serviceMap.begin();
1252
1253 if (serviceMap.size() != 1)
1254 {
1255 return ipmi::responseUnspecifiedError();
1256 }
1257
1258 std::string service = itr->first;
1259 uint8_t sessionState = 0;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001260 completionCode = getSessionState(ctx, service, objectPath,
1261 sessionState);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001262 if (completionCode)
1263 {
1264 return ipmi::response(completionCode);
1265 }
1266
1267 if (sessionState == static_cast<uint8_t>(session::State::active))
1268 {
1269 activeSessionCount++;
1270 }
1271
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001272 if (index == sessionIndex || reqSessionId == sessionId ||
1273 reqSessionHandle == sessionHandle)
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001274 {
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001275 SessionDetails details{};
1276 completionCode = getSessionDetails(ctx, service, objectPath,
1277 sessionHandle, state, details);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001278
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001279 if (completionCode)
1280 {
1281 return ipmi::response(completionCode);
1282 }
1283 activeSessionHandle = sessionHandle;
1284 maybeDetails = std::move(details);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001285 }
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001286 }
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001287
1288 if (state == static_cast<uint8_t>(session::State::active) ||
1289 state == static_cast<uint8_t>(session::State::tearDownInProgress))
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001290 {
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001291 return ipmi::responseSuccess(activeSessionHandle, totalSessionCount,
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001292 activeSessionCount, maybeDetails);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001293 }
1294
1295 return ipmi::responseInvalidFieldRequest();
1296}
1297
Xo Wangf542e8b2017-08-09 15:34:16 -07001298static std::unique_ptr<SysInfoParamStore> sysInfoParamStore;
1299
Xo Wang87651332017-08-11 10:17:59 -07001300static std::string sysInfoReadSystemName()
1301{
1302 // Use the BMC hostname as the "System Name."
1303 char hostname[HOST_NAME_MAX + 1] = {};
1304 if (gethostname(hostname, HOST_NAME_MAX) != 0)
1305 {
1306 perror("System info parameter: system name");
1307 }
1308 return hostname;
1309}
1310
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001311static constexpr uint8_t paramRevision = 0x11;
1312static constexpr size_t configParameterLength = 16;
Xo Wangf542e8b2017-08-09 15:34:16 -07001313
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001314static constexpr size_t smallChunkSize = 14;
1315static constexpr size_t fullChunkSize = 16;
Jia, chunhui449f2162019-09-11 16:51:51 +08001316static constexpr uint8_t progressMask = 0x3;
krishnar410752832023-11-06 13:58:35 +05301317static constexpr uint8_t maxValidEncodingData = 0x02;
Xo Wangf542e8b2017-08-09 15:34:16 -07001318
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001319static constexpr uint8_t setComplete = 0x0;
1320static constexpr uint8_t setInProgress = 0x1;
1321static constexpr uint8_t commitWrite = 0x2;
1322static uint8_t transferStatus = setComplete;
1323
Jia, chunhui449f2162019-09-11 16:51:51 +08001324static constexpr uint8_t configDataOverhead = 2;
1325
1326// For EFI based system, 256 bytes is recommended.
1327static constexpr size_t maxBytesPerParameter = 256;
1328
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001329namespace ipmi
1330{
1331constexpr Cc ccParmNotSupported = 0x80;
Jia, chunhui449f2162019-09-11 16:51:51 +08001332constexpr Cc ccSetInProgressActive = 0x81;
1333constexpr Cc ccSystemInfoParameterSetReadOnly = 0x82;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001334
1335static inline auto responseParmNotSupported()
1336{
1337 return response(ccParmNotSupported);
Xo Wangf542e8b2017-08-09 15:34:16 -07001338}
Jia, chunhui449f2162019-09-11 16:51:51 +08001339static inline auto responseSetInProgressActive()
1340{
1341 return response(ccSetInProgressActive);
1342}
1343static inline auto responseSystemInfoParameterSetReadOnly()
1344{
1345 return response(ccSystemInfoParameterSetReadOnly);
1346}
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001347} // namespace ipmi
Xo Wangf542e8b2017-08-09 15:34:16 -07001348
Jia, chunhui449f2162019-09-11 16:51:51 +08001349ipmi::RspType<uint8_t, // Parameter revision
1350 std::optional<uint8_t>, // data1 / setSelector / ProgressStatus
1351 std::optional<std::vector<uint8_t>>> // data2-17
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001352 ipmiAppGetSystemInfo(uint7_t reserved, bool getRevision,
1353 uint8_t paramSelector, uint8_t setSelector,
1354 uint8_t BlockSelector)
Xo Wangf542e8b2017-08-09 15:34:16 -07001355{
Snehalatha Va5ae7722020-05-02 18:18:57 +00001356 if (reserved || (paramSelector >= invalidParamSelectorStart &&
1357 paramSelector <= invalidParamSelectorEnd))
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001358 {
1359 return ipmi::responseInvalidFieldRequest();
1360 }
PavanKumarIntel3771f5f2023-11-02 06:26:42 +00001361 if (paramSelector >= oemCmdStart)
Snehalatha Va5ae7722020-05-02 18:18:57 +00001362 {
1363 return ipmi::responseParmNotSupported();
1364 }
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001365 if (getRevision)
Xo Wangf542e8b2017-08-09 15:34:16 -07001366 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001367 return ipmi::responseSuccess(paramRevision, std::nullopt, std::nullopt);
Xo Wangf542e8b2017-08-09 15:34:16 -07001368 }
1369
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001370 if (paramSelector == 0)
Xo Wangf542e8b2017-08-09 15:34:16 -07001371 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001372 return ipmi::responseSuccess(paramRevision, transferStatus,
1373 std::nullopt);
Xo Wangf542e8b2017-08-09 15:34:16 -07001374 }
1375
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001376 if (BlockSelector != 0) // 00h if parameter does not require a block number
Xo Wangf542e8b2017-08-09 15:34:16 -07001377 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001378 return ipmi::responseParmNotSupported();
Xo Wangf542e8b2017-08-09 15:34:16 -07001379 }
1380
1381 if (sysInfoParamStore == nullptr)
1382 {
1383 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
Xo Wang87651332017-08-11 10:17:59 -07001384 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1385 sysInfoReadSystemName);
Xo Wangf542e8b2017-08-09 15:34:16 -07001386 }
1387
1388 // Parameters other than Set In Progress are assumed to be strings.
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001389 std::tuple<bool, std::string> ret =
1390 sysInfoParamStore->lookup(paramSelector);
1391 bool found = std::get<0>(ret);
Xo Wangf542e8b2017-08-09 15:34:16 -07001392 if (!found)
1393 {
Snehalatha Va5ae7722020-05-02 18:18:57 +00001394 return ipmi::responseSensorInvalid();
Xo Wangf542e8b2017-08-09 15:34:16 -07001395 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001396 std::string& paramString = std::get<1>(ret);
Jia, chunhui449f2162019-09-11 16:51:51 +08001397 std::vector<uint8_t> configData;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001398 size_t count = 0;
1399 if (setSelector == 0)
Jia, chunhui449f2162019-09-11 16:51:51 +08001400 { // First chunk has only 14 bytes.
1401 configData.emplace_back(0); // encoding
1402 configData.emplace_back(paramString.length()); // string length
1403 count = std::min(paramString.length(), smallChunkSize);
1404 configData.resize(count + configDataOverhead);
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001405 std::copy_n(paramString.begin(), count,
Snehalatha Va5ae7722020-05-02 18:18:57 +00001406 configData.begin() + configDataOverhead); // 14 bytes chunk
1407
1408 // Append zero's to remaining bytes
1409 if (configData.size() < configParameterLength)
1410 {
1411 std::fill_n(std::back_inserter(configData),
1412 configParameterLength - configData.size(), 0x00);
1413 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001414 }
1415 else
Xo Wangf542e8b2017-08-09 15:34:16 -07001416 {
Jia, chunhui449f2162019-09-11 16:51:51 +08001417 size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001418 if (offset >= paramString.length())
1419 {
1420 return ipmi::responseParmOutOfRange();
1421 }
Jia, chunhui449f2162019-09-11 16:51:51 +08001422 count = std::min(paramString.length() - offset, fullChunkSize);
1423 configData.resize(count);
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001424 std::copy_n(paramString.begin() + offset, count,
1425 configData.begin()); // 16 bytes chunk
Xo Wangf542e8b2017-08-09 15:34:16 -07001426 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001427 return ipmi::responseSuccess(paramRevision, setSelector, configData);
Xo Wangf542e8b2017-08-09 15:34:16 -07001428}
1429
Jia, chunhui449f2162019-09-11 16:51:51 +08001430ipmi::RspType<> ipmiAppSetSystemInfo(uint8_t paramSelector, uint8_t data1,
1431 std::vector<uint8_t> configData)
1432{
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001433 if (paramSelector >= invalidParamSelectorStart &&
1434 paramSelector <= invalidParamSelectorEnd)
1435 {
1436 return ipmi::responseInvalidFieldRequest();
1437 }
PavanKumarIntel3771f5f2023-11-02 06:26:42 +00001438 if (paramSelector >= oemCmdStart)
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001439 {
1440 return ipmi::responseParmNotSupported();
1441 }
1442
Jia, chunhui449f2162019-09-11 16:51:51 +08001443 if (paramSelector == 0)
1444 {
1445 // attempt to set the 'set in progress' value (in parameter #0)
1446 // when not in the set complete state.
1447 if ((transferStatus != setComplete) && (data1 == setInProgress))
1448 {
1449 return ipmi::responseSetInProgressActive();
1450 }
1451 // only following 2 states are supported
1452 if (data1 > setInProgress)
1453 {
George Liu24fffdc2024-07-17 17:40:53 +08001454 lg2::error("illegal SetInProgress status");
Jia, chunhui449f2162019-09-11 16:51:51 +08001455 return ipmi::responseInvalidFieldRequest();
1456 }
1457
1458 transferStatus = data1 & progressMask;
1459 return ipmi::responseSuccess();
1460 }
1461
1462 if (configData.size() > configParameterLength)
1463 {
1464 return ipmi::responseInvalidFieldRequest();
1465 }
1466
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001467 // Append zero's to remaining bytes
1468 if (configData.size() < configParameterLength)
1469 {
1470 fill_n(back_inserter(configData),
1471 (configParameterLength - configData.size()), 0x00);
1472 }
1473
Jia, chunhui449f2162019-09-11 16:51:51 +08001474 if (!sysInfoParamStore)
1475 {
1476 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
1477 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1478 sysInfoReadSystemName);
1479 }
1480
1481 // lookup
1482 std::tuple<bool, std::string> ret =
1483 sysInfoParamStore->lookup(paramSelector);
1484 bool found = std::get<0>(ret);
1485 std::string& paramString = std::get<1>(ret);
1486 if (!found)
1487 {
1488 // parameter does not exist. Init new
1489 paramString = "";
1490 }
1491
1492 uint8_t setSelector = data1;
1493 size_t count = 0;
krishnar410752832023-11-06 13:58:35 +05301494 if (setSelector == 0) // First chunk has only 14 bytes.
Jia, chunhui449f2162019-09-11 16:51:51 +08001495 {
krishnar410752832023-11-06 13:58:35 +05301496 uint8_t encoding = configData.at(0);
1497 if (encoding > maxValidEncodingData)
1498 {
1499 return ipmi::responseInvalidFieldRequest();
1500 }
1501
Jia, chunhui449f2162019-09-11 16:51:51 +08001502 size_t stringLen = configData.at(1); // string length
1503 // maxBytesPerParamter is 256. It will always be greater than stringLen
1504 // (unit8_t) if maxBytes changes in future, then following line is
1505 // needed.
1506 // stringLen = std::min(stringLen, maxBytesPerParameter);
1507 count = std::min(stringLen, smallChunkSize);
1508 count = std::min(count, configData.size());
1509 paramString.resize(stringLen); // reserve space
1510 std::copy_n(configData.begin() + configDataOverhead, count,
1511 paramString.begin());
1512 }
1513 else
1514 {
1515 size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
1516 if (offset >= paramString.length())
1517 {
1518 return ipmi::responseParmOutOfRange();
1519 }
1520 count = std::min(paramString.length() - offset, configData.size());
1521 std::copy_n(configData.begin(), count, paramString.begin() + offset);
1522 }
1523 sysInfoParamStore->update(paramSelector, paramString);
1524 return ipmi::responseSuccess();
1525}
1526
Yong Libd0503a2019-08-22 17:17:17 +08001527#ifdef ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301528inline std::vector<uint8_t> convertStringToData(const std::string& command)
1529{
1530 std::istringstream iss(command);
1531 std::string token;
1532 std::vector<uint8_t> dataValue;
1533 while (std::getline(iss, token, ' '))
1534 {
1535 dataValue.emplace_back(
1536 static_cast<uint8_t>(std::stoul(token, nullptr, base_16)));
1537 }
1538 return dataValue;
1539}
1540
Matt Simmering68d9d402023-11-09 14:22:11 -08001541static bool populateI2CControllerWRAllowlist()
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301542{
1543 nlohmann::json data = nullptr;
Matt Simmering68d9d402023-11-09 14:22:11 -08001544 std::ifstream jsonFile(i2cControllerWRAllowlistFile);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301545
1546 if (!jsonFile.good())
1547 {
George Liu24fffdc2024-07-17 17:40:53 +08001548 lg2::warning("i2c allow list file not found! file name: {FILE_NAME}",
1549 "FILE_NAME", i2cControllerWRAllowlistFile);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301550 return false;
1551 }
1552
1553 try
1554 {
1555 data = nlohmann::json::parse(jsonFile, nullptr, false);
1556 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001557 catch (const nlohmann::json::parse_error& e)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301558 {
George Liu24fffdc2024-07-17 17:40:53 +08001559 lg2::error("Corrupted i2c allow list config file, "
1560 "file name: {FILE_NAME}, error: {ERROR}",
1561 "FILE_NAME", i2cControllerWRAllowlistFile, "ERROR", e);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301562 return false;
1563 }
1564
1565 try
1566 {
1567 // Example JSON Structure format
1568 // "filters": [
1569 // {
1570 // "Description": "Allow full read - ignore first byte write value
1571 // for 0x40 to 0x4F",
1572 // "busId": "0x01",
1573 // "slaveAddr": "0x40",
1574 // "slaveAddrMask": "0x0F",
1575 // "command": "0x00",
1576 // "commandMask": "0xFF"
1577 // },
1578 // {
1579 // "Description": "Allow full read - first byte match 0x05 and
1580 // ignore second byte",
1581 // "busId": "0x01",
1582 // "slaveAddr": "0x57",
1583 // "slaveAddrMask": "0x00",
1584 // "command": "0x05 0x00",
1585 // "commandMask": "0x00 0xFF"
1586 // },]
1587
1588 nlohmann::json filters = data[filtersStr].get<nlohmann::json>();
Matt Simmering68d9d402023-11-09 14:22:11 -08001589 std::vector<i2cControllerWRAllowlist>& allowlist = getWRAllowlist();
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301590 for (const auto& it : filters.items())
1591 {
1592 nlohmann::json filter = it.value();
1593 if (filter.is_null())
1594 {
George Liu24fffdc2024-07-17 17:40:53 +08001595 lg2::error(
1596 "Corrupted I2C controller write read allowlist config file, "
1597 "file name: {FILE_NAME}",
1598 "FILE_NAME", i2cControllerWRAllowlistFile);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301599 return false;
1600 }
1601 const std::vector<uint8_t>& writeData =
1602 convertStringToData(filter[cmdStr].get<std::string>());
1603 const std::vector<uint8_t>& writeDataMask =
1604 convertStringToData(filter[cmdMaskStr].get<std::string>());
1605 if (writeDataMask.size() != writeData.size())
1606 {
George Liu24fffdc2024-07-17 17:40:53 +08001607 lg2::error("I2C controller write read allowlist filter "
1608 "mismatch for command & mask size");
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301609 return false;
1610 }
Matt Simmering68d9d402023-11-09 14:22:11 -08001611 allowlist.push_back(
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301612 {static_cast<uint8_t>(std::stoul(
1613 filter[busIdStr].get<std::string>(), nullptr, base_16)),
1614 static_cast<uint8_t>(
Matt Simmering68d9d402023-11-09 14:22:11 -08001615 std::stoul(filter[targetAddrStr].get<std::string>(),
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301616 nullptr, base_16)),
1617 static_cast<uint8_t>(
Matt Simmering68d9d402023-11-09 14:22:11 -08001618 std::stoul(filter[targetAddrMaskStr].get<std::string>(),
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301619 nullptr, base_16)),
1620 writeData, writeDataMask});
1621 }
Matt Simmering68d9d402023-11-09 14:22:11 -08001622 if (allowlist.size() != filters.size())
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301623 {
George Liu24fffdc2024-07-17 17:40:53 +08001624 lg2::error(
Matt Simmering68d9d402023-11-09 14:22:11 -08001625 "I2C controller write read allowlist filter size mismatch");
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301626 return false;
1627 }
1628 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001629 catch (const std::exception& e)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301630 {
George Liu24fffdc2024-07-17 17:40:53 +08001631 lg2::error("I2C controller write read allowlist "
1632 "unexpected exception: {ERROR}",
1633 "ERROR", e);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301634 return false;
1635 }
1636 return true;
1637}
1638
Matt Simmering68d9d402023-11-09 14:22:11 -08001639static inline bool isWriteDataAllowlisted(const std::vector<uint8_t>& data,
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301640 const std::vector<uint8_t>& dataMask,
1641 const std::vector<uint8_t>& writeData)
1642{
1643 std::vector<uint8_t> processedDataBuf(data.size());
1644 std::vector<uint8_t> processedReqBuf(dataMask.size());
1645 std::transform(writeData.begin(), writeData.end(), dataMask.begin(),
1646 processedReqBuf.begin(), std::bit_or<uint8_t>());
1647 std::transform(data.begin(), data.end(), dataMask.begin(),
1648 processedDataBuf.begin(), std::bit_or<uint8_t>());
1649
1650 return (processedDataBuf == processedReqBuf);
1651}
1652
Matt Simmering68d9d402023-11-09 14:22:11 -08001653static bool isCmdAllowlisted(uint8_t busId, uint8_t targetAddr,
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301654 std::vector<uint8_t>& writeData)
1655{
Matt Simmering68d9d402023-11-09 14:22:11 -08001656 std::vector<i2cControllerWRAllowlist>& allowList = getWRAllowlist();
1657 for (const auto& wlEntry : allowList)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301658 {
1659 if ((busId == wlEntry.busId) &&
Matt Simmering68d9d402023-11-09 14:22:11 -08001660 ((targetAddr | wlEntry.targetAddrMask) ==
1661 (wlEntry.targetAddr | wlEntry.targetAddrMask)))
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301662 {
1663 const std::vector<uint8_t>& dataMask = wlEntry.dataMask;
1664 // Skip as no-match, if requested write data is more than the
1665 // write data mask size
1666 if (writeData.size() > dataMask.size())
1667 {
1668 continue;
1669 }
Matt Simmering68d9d402023-11-09 14:22:11 -08001670 if (isWriteDataAllowlisted(wlEntry.data, dataMask, writeData))
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301671 {
1672 return true;
1673 }
1674 }
1675 }
1676 return false;
1677}
Yong Libd0503a2019-08-22 17:17:17 +08001678#else
Matt Simmering68d9d402023-11-09 14:22:11 -08001679static bool populateI2CControllerWRAllowlist()
Yong Libd0503a2019-08-22 17:17:17 +08001680{
George Liu24fffdc2024-07-17 17:40:53 +08001681 lg2::info("I2C_WHITELIST_CHECK is disabled, do not populate allowlist");
Yong Libd0503a2019-08-22 17:17:17 +08001682 return true;
1683}
1684#endif // ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301685
Matt Simmering68d9d402023-11-09 14:22:11 -08001686/** @brief implements controller write read IPMI command which can be used for
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301687 * low-level I2C/SMBus write, read or write-read access
1688 * @param isPrivateBus -to indicate private bus usage
1689 * @param busId - bus id
1690 * @param channelNum - channel number
1691 * @param reserved - skip 1 bit
Matt Simmering68d9d402023-11-09 14:22:11 -08001692 * @param targetAddr - target address
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301693 * @param read count - number of bytes to be read
1694 * @param writeData - data to be written
1695 *
1696 * @returns IPMI completion code plus response data
1697 * - readData - i2c response data
1698 */
1699ipmi::RspType<std::vector<uint8_t>>
Matt Simmering68d9d402023-11-09 14:22:11 -08001700 ipmiControllerWriteRead([[maybe_unused]] bool isPrivateBus, uint3_t busId,
1701 [[maybe_unused]] uint4_t channelNum, bool reserved,
1702 uint7_t targetAddr, uint8_t readCount,
1703 std::vector<uint8_t> writeData)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301704{
Jayaprakash Mutyalac2af98b2021-09-14 09:19:11 +00001705 if (reserved)
1706 {
1707 return ipmi::responseInvalidFieldRequest();
1708 }
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301709 const size_t writeCount = writeData.size();
1710 if (!readCount && !writeCount)
1711 {
George Liu24fffdc2024-07-17 17:40:53 +08001712 lg2::error("Controller write read command: Read & write count are 0");
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301713 return ipmi::responseInvalidFieldRequest();
1714 }
Yong Libd0503a2019-08-22 17:17:17 +08001715#ifdef ENABLE_I2C_WHITELIST_CHECK
Matt Simmering68d9d402023-11-09 14:22:11 -08001716 if (!isCmdAllowlisted(static_cast<uint8_t>(busId),
1717 static_cast<uint8_t>(targetAddr), writeData))
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301718 {
George Liu24fffdc2024-07-17 17:40:53 +08001719 lg2::error("Controller write read request blocked!, "
1720 "bus: {BUS}, addr: {ADDR}",
1721 "BUS", static_cast<uint8_t>(busId), "ADDR", lg2::hex,
1722 static_cast<uint8_t>(targetAddr));
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301723 }
Yong Libd0503a2019-08-22 17:17:17 +08001724#endif // ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301725 std::vector<uint8_t> readBuf(readCount);
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001726 std::string i2cBus = "/dev/i2c-" +
1727 std::to_string(static_cast<uint8_t>(busId));
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301728
Matt Simmering68d9d402023-11-09 14:22:11 -08001729 ipmi::Cc ret = ipmi::i2cWriteRead(i2cBus, static_cast<uint8_t>(targetAddr),
Yong Li7dc4ac02019-08-23 17:44:32 +08001730 writeData, readBuf);
1731 if (ret != ipmi::ccSuccess)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301732 {
Yong Li7dc4ac02019-08-23 17:44:32 +08001733 return ipmi::response(ret);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301734 }
1735 return ipmi::responseSuccess(readBuf);
1736}
1737
Chris Austen6caf28b2015-10-13 12:40:40 -05001738void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +05301739{
Vernon Mauery86a50822019-03-25 13:11:36 -07001740 // <Get Device ID>
1741 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1742 ipmi::app::cmdGetDeviceId, ipmi::Privilege::User,
1743 ipmiAppGetDeviceId);
1744
Tom05732372016-09-06 17:21:23 +05301745 // <Get BT Interface Capabilities>
Vernon Mauerycc99ba42019-03-25 13:40:11 -07001746 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1747 ipmi::app::cmdGetBtIfaceCapabilities,
1748 ipmi::Privilege::User, ipmiAppGetBtCapabilities);
Chris Austen6caf28b2015-10-13 12:40:40 -05001749
Tom05732372016-09-06 17:21:23 +05301750 // <Reset Watchdog Timer>
Vernon Mauery11df4f62019-03-25 14:17:54 -07001751 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1752 ipmi::app::cmdResetWatchdogTimer,
1753 ipmi::Privilege::Operator, ipmiAppResetWatchdogTimer);
Chris Austen6caf28b2015-10-13 12:40:40 -05001754
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001755 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301756 ipmi::app::cmdGetSessionInfo, ipmi::Privilege::User,
1757 ipmiAppGetSessionInfo);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001758
Tom05732372016-09-06 17:21:23 +05301759 // <Set Watchdog Timer>
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +00001760 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1761 ipmi::app::cmdSetWatchdogTimer,
1762 ipmi::Privilege::Operator, ipmiSetWatchdogTimer);
Chris Austen6caf28b2015-10-13 12:40:40 -05001763
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +00001764 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1765 ipmi::app::cmdCloseSession, ipmi::Privilege::Callback,
1766 ipmiAppCloseSession);
1767
William A. Kennington III73f44512018-02-09 15:28:46 -08001768 // <Get Watchdog Timer>
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +00001769 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301770 ipmi::app::cmdGetWatchdogTimer, ipmi::Privilege::User,
1771 ipmiGetWatchdogTimer);
William A. Kennington III73f44512018-02-09 15:28:46 -08001772
Tom05732372016-09-06 17:21:23 +05301773 // <Get Self Test Results>
Vernon Maueryb84a5282019-03-25 13:39:03 -07001774 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1775 ipmi::app::cmdGetSelfTestResults,
1776 ipmi::Privilege::User, ipmiAppGetSelfTestResults);
Nan Li41fa24a2016-11-10 20:12:37 +08001777
Tom05732372016-09-06 17:21:23 +05301778 // <Get Device GUID>
Vernon Mauery15541322019-03-25 13:33:03 -07001779 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1780 ipmi::app::cmdGetDeviceGuid, ipmi::Privilege::User,
1781 ipmiAppGetDeviceGuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -05001782
Tom05732372016-09-06 17:21:23 +05301783 // <Set ACPI Power State>
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +00001784 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1785 ipmi::app::cmdSetAcpiPowerState,
1786 ipmi::Privilege::Admin, ipmiSetAcpiPowerState);
Yong Li18d77262018-10-09 01:59:45 +08001787 // <Get ACPI Power State>
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +00001788 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1789 ipmi::app::cmdGetAcpiPowerState,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301790 ipmi::Privilege::User, ipmiGetAcpiPowerState);
Yong Li18d77262018-10-09 01:59:45 +08001791
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301792 // Note: For security reason, this command will be registered only when
Matt Simmering68d9d402023-11-09 14:22:11 -08001793 // there are proper I2C Controller write read allowlist
1794 if (populateI2CControllerWRAllowlist())
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301795 {
Matt Simmering68d9d402023-11-09 14:22:11 -08001796 // Note: For security reasons, registering controller write read as
1797 // admin privilege command, even though IPMI 2.0 specification allows it
1798 // as operator privilege.
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301799 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1800 ipmi::app::cmdMasterWriteRead,
Matt Simmering68d9d402023-11-09 14:22:11 -08001801 ipmi::Privilege::Admin, ipmiControllerWriteRead);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301802 }
1803
Marri Devender Rao5e007a52018-01-08 06:18:36 -06001804 // <Get System GUID Command>
Vernon Maueryb90a5322019-03-25 13:36:55 -07001805 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1806 ipmi::app::cmdGetSystemGuid, ipmi::Privilege::User,
1807 ipmiAppGetSystemGuid);
Tom Joseph7cbe2282018-03-21 21:17:33 +05301808
1809 // <Get Channel Cipher Suites Command>
Ayushi Smriti5c3b72c2019-08-30 13:47:31 +00001810 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1811 ipmi::app::cmdGetChannelCipherSuites,
Vernon Mauery79b4eea2019-11-07 09:51:39 -08001812 ipmi::Privilege::None, getChannelCipherSuites);
Vernon Maueryd2a57de2019-03-25 12:45:28 -07001813
Xo Wangf542e8b2017-08-09 15:34:16 -07001814 // <Get System Info Command>
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001815 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1816 ipmi::app::cmdGetSystemInfoParameters,
1817 ipmi::Privilege::User, ipmiAppGetSystemInfo);
Jia, chunhui449f2162019-09-11 16:51:51 +08001818 // <Set System Info Command>
1819 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1820 ipmi::app::cmdSetSystemInfoParameters,
1821 ipmi::Privilege::Admin, ipmiAppSetSystemInfo);
vishwabmcba0bd5f2015-09-30 16:50:23 +05301822 return;
1823}