blob: 0919cfbd9193348185478b1bc2f614b7cddfefb7 [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 Williams1318a5e2024-08-16 15:19:54 -0400117 objectTree =
118 ipmi::getAllDbusObjects(*ctx->bus, softwareRoot, 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 Williams1318a5e2024-08-16 15:19:54 -0400131 auto service =
132 ipmi::getService(*ctx->bus, redundancyIntf, softObject.first);
133 auto objValueTree =
134 ipmi::getManagedObjects(*ctx->bus, service, 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 Williams1318a5e2024-08-16 15:19:54 -0400187 ipmi::DbusObjectInfo bmcObject =
188 ipmi::getDbusObject(bus, bmc_state_interface);
189 auto variant =
190 ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first,
191 bmc_state_interface, 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
Yong Li18d77262018-10-09 01:59:45 +0800216const static constexpr char* acpiInterface =
217 "xyz.openbmc_project.Control.Power.ACPIPowerState";
218const static constexpr char* sysACPIProp = "SysACPIStatus";
219const static constexpr char* devACPIProp = "DevACPIStatus";
220
221enum class PowerStateType : uint8_t
222{
223 sysPowerState = 0x00,
224 devPowerState = 0x01,
225};
226
227// Defined in 20.6 of ipmi doc
228enum class PowerState : uint8_t
229{
230 s0G0D0 = 0x00,
231 s1D1 = 0x01,
232 s2D2 = 0x02,
233 s3D3 = 0x03,
234 s4 = 0x04,
235 s5G2 = 0x05,
236 s4S5 = 0x06,
237 g3 = 0x07,
238 sleep = 0x08,
239 g1Sleep = 0x09,
240 override = 0x0a,
241 legacyOn = 0x20,
242 legacyOff = 0x21,
243 unknown = 0x2a,
244 noChange = 0x7f,
245};
246
247static constexpr uint8_t stateChanged = 0x80;
248
Yong Li18d77262018-10-09 01:59:45 +0800249std::map<ACPIPowerState::ACPI, PowerState> dbusToIPMI = {
250 {ACPIPowerState::ACPI::S0_G0_D0, PowerState::s0G0D0},
251 {ACPIPowerState::ACPI::S1_D1, PowerState::s1D1},
252 {ACPIPowerState::ACPI::S2_D2, PowerState::s2D2},
253 {ACPIPowerState::ACPI::S3_D3, PowerState::s3D3},
254 {ACPIPowerState::ACPI::S4, PowerState::s4},
255 {ACPIPowerState::ACPI::S5_G2, PowerState::s5G2},
256 {ACPIPowerState::ACPI::S4_S5, PowerState::s4S5},
257 {ACPIPowerState::ACPI::G3, PowerState::g3},
258 {ACPIPowerState::ACPI::SLEEP, PowerState::sleep},
259 {ACPIPowerState::ACPI::G1_SLEEP, PowerState::g1Sleep},
260 {ACPIPowerState::ACPI::OVERRIDE, PowerState::override},
261 {ACPIPowerState::ACPI::LEGACY_ON, PowerState::legacyOn},
262 {ACPIPowerState::ACPI::LEGACY_OFF, PowerState::legacyOff},
263 {ACPIPowerState::ACPI::Unknown, PowerState::unknown}};
264
265bool isValidACPIState(acpi_state::PowerStateType type, uint8_t state)
266{
267 if (type == acpi_state::PowerStateType::sysPowerState)
268 {
269 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::override)) ||
270 (state == static_cast<uint8_t>(acpi_state::PowerState::legacyOn)) ||
271 (state ==
272 static_cast<uint8_t>(acpi_state::PowerState::legacyOff)) ||
273 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
274 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
275 {
276 return true;
277 }
278 else
279 {
280 return false;
281 }
282 }
283 else if (type == acpi_state::PowerStateType::devPowerState)
284 {
285 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::s3D3)) ||
286 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
287 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
288 {
289 return true;
290 }
291 else
292 {
293 return false;
294 }
295 }
296 else
297 {
298 return false;
299 }
300 return false;
301}
302} // namespace acpi_state
303
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000304/** @brief implements Set ACPI Power State command
305 * @param sysAcpiState - ACPI system power state to set
306 * @param devAcpiState - ACPI device power state to set
307 *
308 * @return IPMI completion code on success
309 **/
310ipmi::RspType<> ipmiSetAcpiPowerState(uint8_t sysAcpiState,
311 uint8_t devAcpiState)
Chris Austen6caf28b2015-10-13 12:40:40 -0500312{
Yong Li18d77262018-10-09 01:59:45 +0800313 auto s = static_cast<uint8_t>(acpi_state::PowerState::unknown);
Yong Li18d77262018-10-09 01:59:45 +0800314
Patrick Williams5d82f472022-07-22 19:26:53 -0500315 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Yong Li18d77262018-10-09 01:59:45 +0800316
317 auto value = acpi_state::ACPIPowerState::ACPI::Unknown;
318
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000319 if (sysAcpiState & acpi_state::stateChanged)
Yong Li18d77262018-10-09 01:59:45 +0800320 {
321 // set system power state
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000322 s = sysAcpiState & ~acpi_state::stateChanged;
Yong Li18d77262018-10-09 01:59:45 +0800323
324 if (!acpi_state::isValidACPIState(
325 acpi_state::PowerStateType::sysPowerState, s))
326 {
George Liu24fffdc2024-07-17 17:40:53 +0800327 lg2::error("set_acpi_power sys invalid input, S: {S}", "S", s);
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000328 return ipmi::responseParmOutOfRange();
Yong Li18d77262018-10-09 01:59:45 +0800329 }
330
331 // valid input
332 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
333 {
George Liu24fffdc2024-07-17 17:40:53 +0800334 lg2::debug("No change for system power state");
Yong Li18d77262018-10-09 01:59:45 +0800335 }
336 else
337 {
Patrick Williams1318a5e2024-08-16 15:19:54 -0400338 auto found = std::find_if(
339 acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(),
340 [&s](const auto& iter) {
341 return (static_cast<uint8_t>(iter.second) == s);
342 });
Yong Li18d77262018-10-09 01:59:45 +0800343
344 value = found->first;
345
346 try
347 {
348 auto acpiObject =
349 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
350 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
351 acpi_state::acpiInterface,
352 acpi_state::sysACPIProp,
353 convertForMessage(value));
354 }
355 catch (const InternalFailure& e)
356 {
George Liu24fffdc2024-07-17 17:40:53 +0800357 lg2::error("Failed in set ACPI system property: {ERROR}",
358 "ERROR", e);
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000359 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800360 }
361 }
362 }
363 else
364 {
George Liu24fffdc2024-07-17 17:40:53 +0800365 lg2::debug("Do not change system power state");
Yong Li18d77262018-10-09 01:59:45 +0800366 }
367
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000368 if (devAcpiState & acpi_state::stateChanged)
Yong Li18d77262018-10-09 01:59:45 +0800369 {
370 // set device power state
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000371 s = devAcpiState & ~acpi_state::stateChanged;
Yong Li18d77262018-10-09 01:59:45 +0800372 if (!acpi_state::isValidACPIState(
373 acpi_state::PowerStateType::devPowerState, s))
374 {
George Liu24fffdc2024-07-17 17:40:53 +0800375 lg2::error("set_acpi_power dev invalid input, S: {S}", "S", s);
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000376 return ipmi::responseParmOutOfRange();
Yong Li18d77262018-10-09 01:59:45 +0800377 }
378
379 // valid input
380 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
381 {
George Liu24fffdc2024-07-17 17:40:53 +0800382 lg2::debug("No change for device power state");
Yong Li18d77262018-10-09 01:59:45 +0800383 }
384 else
385 {
Patrick Williams1318a5e2024-08-16 15:19:54 -0400386 auto found = std::find_if(
387 acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(),
388 [&s](const auto& iter) {
389 return (static_cast<uint8_t>(iter.second) == s);
390 });
Yong Li18d77262018-10-09 01:59:45 +0800391
392 value = found->first;
393
394 try
395 {
396 auto acpiObject =
397 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
398 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
399 acpi_state::acpiInterface,
400 acpi_state::devACPIProp,
401 convertForMessage(value));
402 }
403 catch (const InternalFailure& e)
404 {
George Liu24fffdc2024-07-17 17:40:53 +0800405 lg2::error("Failed in set ACPI device property: {ERROR}",
406 "ERROR", e);
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000407 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800408 }
409 }
410 }
411 else
412 {
George Liu24fffdc2024-07-17 17:40:53 +0800413 lg2::debug("Do not change device power state");
Yong Li18d77262018-10-09 01:59:45 +0800414 }
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000415 return ipmi::responseSuccess();
Yong Li18d77262018-10-09 01:59:45 +0800416}
417
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000418/**
419 * @brief implements the get ACPI power state command
420 *
421 * @return IPMI completion code plus response data on success.
422 * - ACPI system power state
423 * - ACPI device power state
424 **/
425ipmi::RspType<uint8_t, // acpiSystemPowerState
426 uint8_t // acpiDevicePowerState
427 >
428 ipmiGetAcpiPowerState()
Yong Li18d77262018-10-09 01:59:45 +0800429{
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000430 uint8_t sysAcpiState;
431 uint8_t devAcpiState;
Yong Li18d77262018-10-09 01:59:45 +0800432
Patrick Williams5d82f472022-07-22 19:26:53 -0500433 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Yong Li18d77262018-10-09 01:59:45 +0800434
Yong Li18d77262018-10-09 01:59:45 +0800435 try
436 {
437 auto acpiObject = ipmi::getDbusObject(bus, acpi_state::acpiInterface);
438
439 auto sysACPIVal = ipmi::getDbusProperty(
440 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
441 acpi_state::sysACPIProp);
442 auto sysACPI = acpi_state::ACPIPowerState::convertACPIFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -0700443 std::get<std::string>(sysACPIVal));
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000444 sysAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(sysACPI));
Yong Li18d77262018-10-09 01:59:45 +0800445
446 auto devACPIVal = ipmi::getDbusProperty(
447 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
448 acpi_state::devACPIProp);
449 auto devACPI = acpi_state::ACPIPowerState::convertACPIFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -0700450 std::get<std::string>(devACPIVal));
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000451 devAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(devACPI));
Yong Li18d77262018-10-09 01:59:45 +0800452 }
453 catch (const InternalFailure& e)
454 {
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000455 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800456 }
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000457
458 return ipmi::responseSuccess(sysAcpiState, devAcpiState);
Chris Austen6caf28b2015-10-13 12:40:40 -0500459}
460
Chris Austen7303bdc2016-04-17 11:50:54 -0500461typedef struct
462{
463 char major;
464 char minor;
Potin Laic7c55922023-02-16 10:33:55 +0800465 uint8_t aux[4];
Vernon Mauery86a50822019-03-25 13:11:36 -0700466} Revision;
Chris Austen7303bdc2016-04-17 11:50:54 -0500467
Potin Lai5d214202023-01-16 18:11:49 +0800468/* Use regular expression searching matched pattern X.Y, and convert it to */
469/* Major (X) and Minor (Y) version. */
470/* Example: */
471/* version = 2.14.0-dev */
472/* ^ ^ */
473/* | |---------------- Minor */
474/* |------------------ Major */
475/* */
Potin Laic7c55922023-02-16 10:33:55 +0800476/* Default regex string only tries to match Major and Minor version. */
477/* */
478/* To match more firmware version info, platforms need to define it own */
479/* regex string to match more strings, and assign correct mapping index in */
480/* matches array. */
481/* */
482/* matches[0]: matched index for major ver */
483/* matches[1]: matched index for minor ver */
484/* matches[2]: matched index for aux[0] (set 0 to skip) */
485/* matches[3]: matched index for aux[1] (set 0 to skip) */
486/* matches[4]: matched index for aux[2] (set 0 to skip) */
487/* matches[5]: matched index for aux[3] (set 0 to skip) */
488/* Example: */
489/* regex = "([\d]+).([\d]+).([\d]+)-dev-([\d]+)-g([0-9a-fA-F]{2}) */
490/* ([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})" */
491/* matches = {1,2,5,6,7,8} */
492/* version = 2.14.0-dev-750-g37a7c5ad1-dirty */
493/* ^ ^ ^ ^ ^ ^ ^ ^ */
494/* | | | | | | | | */
495/* | | | | | | | |-- Aux byte 3 (0xAD), index 8 */
496/* | | | | | | |---- Aux byte 2 (0xC5), index 7 */
497/* | | | | | |------ Aux byte 1 (0xA7), index 6 */
498/* | | | | |-------- Aux byte 0 (0x37), index 5 */
499/* | | | |------------- Not used, index 4 */
500/* | | |------------------- Not used, index 3 */
501/* | |---------------------- Minor (14), index 2 */
502/* |------------------------ Major (2), index 1 */
Potin Lai5d214202023-01-16 18:11:49 +0800503int convertVersion(std::string s, Revision& rev)
Chris Austen7303bdc2016-04-17 11:50:54 -0500504{
Potin Laic7c55922023-02-16 10:33:55 +0800505 static const std::vector<size_t> matches = {
506 MAJOR_MATCH_INDEX, MINOR_MATCH_INDEX, AUX_0_MATCH_INDEX,
507 AUX_1_MATCH_INDEX, AUX_2_MATCH_INDEX, AUX_3_MATCH_INDEX};
Potin Lai5d214202023-01-16 18:11:49 +0800508 std::regex fw_regex(FW_VER_REGEX);
509 std::smatch m;
510 Revision r = {0};
511 size_t val;
Chris Austen7303bdc2016-04-17 11:50:54 -0500512
Potin Laidcde04e2023-03-16 18:44:15 +0800513 if (std::regex_search(s, m, fw_regex))
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600514 {
Potin Laic7c55922023-02-16 10:33:55 +0800515 if (m.size() < *std::max_element(matches.begin(), matches.end()))
516 { // max index higher than match count
Potin Laidcde04e2023-03-16 18:44:15 +0800517 return -1;
518 }
519
Potin Lai5d214202023-01-16 18:11:49 +0800520 // convert major
521 {
Potin Laic7c55922023-02-16 10:33:55 +0800522 std::string_view str = m[matches[0]].str();
Potin Lai5d214202023-01-16 18:11:49 +0800523 auto [ptr, ec]{std::from_chars(str.begin(), str.end(), val)};
524 if (ec != std::errc() || ptr != str.begin() + str.size())
525 { // failed to convert major string
Potin Laidcde04e2023-03-16 18:44:15 +0800526 return -1;
Potin Lai5d214202023-01-16 18:11:49 +0800527 }
Potin Lai058e2912023-09-07 10:16:38 +0800528
529 if (val >= 2000)
530 { // For the platforms use year as major version, it would expect to
531 // have major version between 0 - 99. If the major version is
532 // greater than or equal to 2000, it is treated as a year and
533 // converted to 0 - 99.
534 r.major = val % 100;
535 }
536 else
537 {
538 r.major = val & 0x7F;
539 }
Potin Lai5d214202023-01-16 18:11:49 +0800540 }
541
542 // convert minor
543 {
Potin Laic7c55922023-02-16 10:33:55 +0800544 std::string_view str = m[matches[1]].str();
Potin Lai5d214202023-01-16 18:11:49 +0800545 auto [ptr, ec]{std::from_chars(str.begin(), str.end(), val)};
546 if (ec != std::errc() || ptr != str.begin() + str.size())
547 { // failed to convert minor string
Potin Laidcde04e2023-03-16 18:44:15 +0800548 return -1;
Potin Lai5d214202023-01-16 18:11:49 +0800549 }
550 r.minor = val & 0xFF;
551 }
552
Potin Laic7c55922023-02-16 10:33:55 +0800553 // convert aux bytes
554 {
555 size_t i;
556 for (i = 0; i < 4; i++)
557 {
558 if (matches[i + 2] == 0)
559 {
560 continue;
561 }
562
563 std::string_view str = m[matches[i + 2]].str();
564 auto [ptr,
565 ec]{std::from_chars(str.begin(), str.end(), val, 16)};
566 if (ec != std::errc() || ptr != str.begin() + str.size())
567 { // failed to convert aux byte string
568 break;
569 }
570
571 r.aux[i] = val & 0xFF;
572 }
573
574 if (i != 4)
575 { // something wrong durign converting aux bytes
576 return -1;
577 }
578 }
579
Potin Lai5d214202023-01-16 18:11:49 +0800580 // all matched
581 rev = r;
582 return 0;
Chris Austen176c9652016-04-30 16:32:17 -0500583 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500584
Potin Lai5d214202023-01-16 18:11:49 +0800585 return -1;
Chris Austen7303bdc2016-04-17 11:50:54 -0500586}
587
Vernon Maueryea1c4012019-05-24 13:26:16 -0700588/* @brief: Implement the Get Device ID IPMI command per the IPMI spec
589 * @param[in] ctx - shared_ptr to an IPMI context struct
590 *
591 * @returns IPMI completion code plus response data
592 * - Device ID (manufacturer defined)
593 * - Device revision[4 bits]; reserved[3 bits]; SDR support[1 bit]
594 * - FW revision major[7 bits] (binary encoded); available[1 bit]
595 * - FW Revision minor (BCD encoded)
596 * - IPMI version (0x02 for IPMI 2.0)
597 * - device support (bitfield of supported options)
598 * - MFG IANA ID (3 bytes)
599 * - product ID (2 bytes)
600 * - AUX info (4 bytes)
601 */
602ipmi::RspType<uint8_t, // Device ID
603 uint8_t, // Device Revision
604 uint8_t, // Firmware Revision Major
605 uint8_t, // Firmware Revision minor
606 uint8_t, // IPMI version
607 uint8_t, // Additional device support
608 uint24_t, // MFG ID
609 uint16_t, // Product ID
610 uint32_t // AUX info
611 >
Willy Tu11d68892022-01-20 10:37:34 -0800612 ipmiAppGetDeviceId([[maybe_unused]] ipmi::Context::ptr ctx)
Chris Austen6caf28b2015-10-13 12:40:40 -0500613{
Vernon Mauery86a50822019-03-25 13:11:36 -0700614 static struct
615 {
616 uint8_t id;
617 uint8_t revision;
618 uint8_t fw[2];
619 uint8_t ipmiVer;
620 uint8_t addnDevSupport;
621 uint24_t manufId;
622 uint16_t prodId;
623 uint32_t aux;
624 } devId;
David Cobbleya1adb072017-11-21 15:58:13 -0800625 static bool dev_id_initialized = false;
Patrick Venture94930a12019-04-30 10:01:58 -0700626 static bool defaultActivationSetting = true;
David Cobbleya1adb072017-11-21 15:58:13 -0800627 const char* filename = "/usr/share/ipmi-providers/dev_id.json";
Alexander Amelkinba19c182018-09-04 15:49:36 +0300628 constexpr auto ipmiDevIdStateShift = 7;
629 constexpr auto ipmiDevIdFw1Mask = ~(1 << ipmiDevIdStateShift);
Willy Tub78184e2022-10-27 22:57:38 +0000630
631#ifdef GET_DBUS_ACTIVE_SOFTWARE
632 static bool haveBMCVersion = false;
JeffLin27a62ec2021-11-24 15:40:33 +0800633 if (!haveBMCVersion || !dev_id_initialized)
David Cobbleya1adb072017-11-21 15:58:13 -0800634 {
Willy Tub78184e2022-10-27 22:57:38 +0000635 int r = -1;
636 Revision rev = {0, 0, 0, 0};
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600637 try
638 {
Vernon Maueryea1c4012019-05-24 13:26:16 -0700639 auto version = getActiveSoftwareVersionInfo(ctx);
Vernon Mauery86a50822019-03-25 13:11:36 -0700640 r = convertVersion(version, rev);
David Cobbleya1adb072017-11-21 15:58:13 -0800641 }
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600642 catch (const std::exception& e)
643 {
George Liu24fffdc2024-07-17 17:40:53 +0800644 lg2::error("error message: {ERROR}", "ERROR", e);
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600645 }
Nan Liee0cb902016-07-11 15:38:03 +0800646
Patrick Venture0b02be92018-08-31 11:55:55 -0700647 if (r >= 0)
648 {
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600649 // bit7 identifies if the device is available
650 // 0=normal operation
651 // 1=device firmware, SDR update,
652 // or self-initialization in progress.
Alexander Amelkinba19c182018-09-04 15:49:36 +0300653 // The availability may change in run time, so mask here
654 // and initialize later.
Vernon Mauery86a50822019-03-25 13:11:36 -0700655 devId.fw[0] = rev.major & ipmiDevIdFw1Mask;
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600656
657 rev.minor = (rev.minor > 99 ? 99 : rev.minor);
Vernon Mauery86a50822019-03-25 13:11:36 -0700658 devId.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16;
Potin Laic7c55922023-02-16 10:33:55 +0800659 std::memcpy(&devId.aux, rev.aux, sizeof(rev.aux));
Brandon Kimc1f5aca2022-03-17 17:54:06 -0700660 haveBMCVersion = true;
David Cobbleya1adb072017-11-21 15:58:13 -0800661 }
JeffLin27a62ec2021-11-24 15:40:33 +0800662 }
Willy Tub78184e2022-10-27 22:57:38 +0000663#endif
JeffLin27a62ec2021-11-24 15:40:33 +0800664 if (!dev_id_initialized)
665 {
David Cobbleya1adb072017-11-21 15:58:13 -0800666 // IPMI Spec version 2.0
Vernon Mauery86a50822019-03-25 13:11:36 -0700667 devId.ipmiVer = 2;
Adriana Kobylak0e912642016-06-22 16:54:39 -0500668
Vernon Mauery86a50822019-03-25 13:11:36 -0700669 std::ifstream devIdFile(filename);
670 if (devIdFile.is_open())
David Cobbleya1adb072017-11-21 15:58:13 -0800671 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700672 auto data = nlohmann::json::parse(devIdFile, nullptr, false);
David Cobbleya1adb072017-11-21 15:58:13 -0800673 if (!data.is_discarded())
674 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700675 devId.id = data.value("id", 0);
676 devId.revision = data.value("revision", 0);
677 devId.addnDevSupport = data.value("addn_dev_support", 0);
678 devId.manufId = data.value("manuf_id", 0);
679 devId.prodId = data.value("prod_id", 0);
Potin Lai9a686362023-09-05 09:27:53 +0800680#ifdef GET_DBUS_ACTIVE_SOFTWARE
681 if (!(AUX_0_MATCH_INDEX || AUX_1_MATCH_INDEX ||
682 AUX_2_MATCH_INDEX || AUX_3_MATCH_INDEX))
683#endif
684 {
685 devId.aux = data.value("aux", 0);
686 }
David Cobbleya1adb072017-11-21 15:58:13 -0800687
Willy Tubfd3a172022-05-31 13:57:54 -0700688 if (data.contains("firmware_revision"))
689 {
690 const auto& firmwareRevision = data.at("firmware_revision");
691 if (firmwareRevision.contains("major"))
692 {
693 firmwareRevision.at("major").get_to(devId.fw[0]);
694 }
695 if (firmwareRevision.contains("minor"))
696 {
697 firmwareRevision.at("minor").get_to(devId.fw[1]);
698 }
699 }
700
Patrick Venture94930a12019-04-30 10:01:58 -0700701 // Set the availablitity of the BMC.
702 defaultActivationSetting = data.value("availability", true);
703
Patrick Venture0b02be92018-08-31 11:55:55 -0700704 // Don't read the file every time if successful
David Cobbleya1adb072017-11-21 15:58:13 -0800705 dev_id_initialized = true;
706 }
707 else
708 {
George Liu24fffdc2024-07-17 17:40:53 +0800709 lg2::error("Device ID JSON parser failure");
Vernon Maueryf2587fc2019-04-09 14:28:15 -0700710 return ipmi::responseUnspecifiedError();
David Cobbleya1adb072017-11-21 15:58:13 -0800711 }
712 }
713 else
714 {
George Liu24fffdc2024-07-17 17:40:53 +0800715 lg2::error("Device ID file not found");
Vernon Maueryf2587fc2019-04-09 14:28:15 -0700716 return ipmi::responseUnspecifiedError();
Chris Austen7303bdc2016-04-17 11:50:54 -0500717 }
718 }
Chris Austen6caf28b2015-10-13 12:40:40 -0500719
Alexander Amelkinba19c182018-09-04 15:49:36 +0300720 // Set availability to the actual current BMC state
Vernon Mauery86a50822019-03-25 13:11:36 -0700721 devId.fw[0] &= ipmiDevIdFw1Mask;
Patrick Venture94930a12019-04-30 10:01:58 -0700722 if (!getCurrentBmcStateWithFallback(defaultActivationSetting))
Alexander Amelkinba19c182018-09-04 15:49:36 +0300723 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700724 devId.fw[0] |= (1 << ipmiDevIdStateShift);
Alexander Amelkinba19c182018-09-04 15:49:36 +0300725 }
726
Vernon Mauery86a50822019-03-25 13:11:36 -0700727 return ipmi::responseSuccess(
728 devId.id, devId.revision, devId.fw[0], devId.fw[1], devId.ipmiVer,
729 devId.addnDevSupport, devId.manufId, devId.prodId, devId.aux);
Chris Austen6caf28b2015-10-13 12:40:40 -0500730}
731
Vernon Maueryb84a5282019-03-25 13:39:03 -0700732auto ipmiAppGetSelfTestResults() -> ipmi::RspType<uint8_t, uint8_t>
Nan Li41fa24a2016-11-10 20:12:37 +0800733{
Nan Li41fa24a2016-11-10 20:12:37 +0800734 // Byte 2:
735 // 55h - No error.
Gunnar Mills8991dd62017-10-25 17:11:29 -0500736 // 56h - Self Test function not implemented in this controller.
Nan Li41fa24a2016-11-10 20:12:37 +0800737 // 57h - Corrupted or inaccesssible data or devices.
738 // 58h - Fatal hardware error.
739 // FFh - reserved.
740 // all other: Device-specific 'internal failure'.
741 // Byte 3:
742 // For byte 2 = 55h, 56h, FFh: 00h
743 // For byte 2 = 58h, all other: Device-specific
744 // For byte 2 = 57h: self-test error bitfield.
745 // Note: returning 57h does not imply that all test were run.
746 // [7] 1b = Cannot access SEL device.
747 // [6] 1b = Cannot access SDR Repository.
748 // [5] 1b = Cannot access BMC FRU device.
749 // [4] 1b = IPMB signal lines do not respond.
750 // [3] 1b = SDR Repository empty.
751 // [2] 1b = Internal Use Area of BMC FRU corrupted.
752 // [1] 1b = controller update 'boot block' firmware corrupted.
753 // [0] 1b = controller operational firmware corrupted.
Vernon Maueryb84a5282019-03-25 13:39:03 -0700754 constexpr uint8_t notImplemented = 0x56;
755 constexpr uint8_t zero = 0;
756 return ipmi::responseSuccess(notImplemented, zero);
Nan Li41fa24a2016-11-10 20:12:37 +0800757}
758
Vernon Mauery15541322019-03-25 13:33:03 -0700759static constexpr size_t uuidBinaryLength = 16;
760static std::array<uint8_t, uuidBinaryLength> rfc4122ToIpmi(std::string rfc4122)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500761{
Willy Tu523e2d12023-09-05 11:36:48 -0700762 using Argument = xyz::openbmc_project::common::InvalidArgument;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500763 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
Patrick Ventured2117022018-02-06 08:54:37 -0800764 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte
765 // order
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500766 // Ex: 0x2332fc2c40e66298e511f2782395a361
Vernon Mauery15541322019-03-25 13:33:03 -0700767 constexpr size_t uuidHexLength = (2 * uuidBinaryLength);
768 constexpr size_t uuidRfc4122Length = (uuidHexLength + 4);
769 std::array<uint8_t, uuidBinaryLength> uuid;
770 if (rfc4122.size() == uuidRfc4122Length)
Patrick Venture0b02be92018-08-31 11:55:55 -0700771 {
Vernon Mauery15541322019-03-25 13:33:03 -0700772 rfc4122.erase(std::remove(rfc4122.begin(), rfc4122.end(), '-'),
773 rfc4122.end());
Sergey Solomineb9b8142016-08-23 09:07:28 -0500774 }
Vernon Mauery15541322019-03-25 13:33:03 -0700775 if (rfc4122.size() != uuidHexLength)
vishwa1eaea4f2016-02-26 11:57:40 -0600776 {
Vernon Mauery15541322019-03-25 13:33:03 -0700777 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
778 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
vishwa1eaea4f2016-02-26 11:57:40 -0600779 }
Vernon Mauery15541322019-03-25 13:33:03 -0700780 for (size_t ind = 0; ind < uuidHexLength; ind += 2)
vishwa1eaea4f2016-02-26 11:57:40 -0600781 {
Vernon Mauery15541322019-03-25 13:33:03 -0700782 char v[3];
783 v[0] = rfc4122[ind];
784 v[1] = rfc4122[ind + 1];
785 v[2] = 0;
786 size_t err;
787 long b;
788 try
Emily Shafferedb8bb02018-09-27 14:50:15 -0700789 {
Vernon Mauery15541322019-03-25 13:33:03 -0700790 b = std::stoul(v, &err, 16);
Emily Shafferedb8bb02018-09-27 14:50:15 -0700791 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500792 catch (const std::exception& e)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500793 {
Vernon Mauery15541322019-03-25 13:33:03 -0700794 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
795 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500796 }
Vernon Mauery15541322019-03-25 13:33:03 -0700797 // check that exactly two ascii bytes were converted
798 if (err != 2)
799 {
800 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
801 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
802 }
803 uuid[uuidBinaryLength - (ind / 2) - 1] = static_cast<uint8_t>(b);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500804 }
Vernon Mauery15541322019-03-25 13:33:03 -0700805 return uuid;
806}
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500807
Vernon Mauery15541322019-03-25 13:33:03 -0700808auto ipmiAppGetDeviceGuid()
809 -> ipmi::RspType<std::array<uint8_t, uuidBinaryLength>>
810{
811 // return a fixed GUID based on /etc/machine-id
812 // This should match the /redfish/v1/Managers/bmc's UUID data
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500813
Vernon Mauery15541322019-03-25 13:33:03 -0700814 // machine specific application ID (for BMC ID)
815 // generated by systemd-id128 -p new as per man page
816 static constexpr sd_id128_t bmcUuidAppId = SD_ID128_MAKE(
817 e0, e1, 73, 76, 64, 61, 47, da, a5, 0c, d0, cc, 64, 12, 45, 78);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500818
Vernon Mauery15541322019-03-25 13:33:03 -0700819 sd_id128_t bmcUuid;
820 // create the UUID from /etc/machine-id via the systemd API
821 sd_id128_get_machine_app_specific(bmcUuidAppId, &bmcUuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500822
Vernon Mauery15541322019-03-25 13:33:03 -0700823 char bmcUuidCstr[SD_ID128_STRING_MAX];
824 std::string systemUuid = sd_id128_to_string(bmcUuid, bmcUuidCstr);
825
826 std::array<uint8_t, uuidBinaryLength> uuid = rfc4122ToIpmi(systemUuid);
827 return ipmi::responseSuccess(uuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500828}
Chris Austen6caf28b2015-10-13 12:40:40 -0500829
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700830auto ipmiAppGetBtCapabilities()
831 -> ipmi::RspType<uint8_t, uint8_t, uint8_t, uint8_t, uint8_t>
vishwabmcba0bd5f2015-09-30 16:50:23 +0530832{
Adriana Kobylak88ad8152016-12-13 10:09:08 -0600833 // Per IPMI 2.0 spec, the input and output buffer size must be the max
834 // buffer size minus one byte to allocate space for the length byte.
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700835 constexpr uint8_t nrOutstanding = 0x01;
836 constexpr uint8_t inputBufferSize = MAX_IPMI_BUFFER - 1;
837 constexpr uint8_t outputBufferSize = MAX_IPMI_BUFFER - 1;
838 constexpr uint8_t transactionTime = 0x0A;
839 constexpr uint8_t nrRetries = 0x01;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530840
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700841 return ipmi::responseSuccess(nrOutstanding, inputBufferSize,
842 outputBufferSize, transactionTime, nrRetries);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530843}
844
Vernon Mauery6c44a942023-07-27 11:00:10 -0700845auto ipmiAppGetSystemGuid(ipmi::Context::ptr& ctx)
846 -> ipmi::RspType<std::array<uint8_t, 16>>
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600847{
Vernon Maueryb90a5322019-03-25 13:36:55 -0700848 static constexpr auto uuidInterface = "xyz.openbmc_project.Common.UUID";
849 static constexpr auto uuidProperty = "UUID";
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600850
Vernon Mauery6c44a942023-07-27 11:00:10 -0700851 // Get the Inventory object implementing BMC interface
852 ipmi::DbusObjectInfo objectInfo{};
Hieu Huynhd386fba2024-09-10 03:14:12 +0000853 boost::system::error_code ec = ipmi::getDbusObject(
854 ctx, uuidInterface, ipmi::sensor::inventoryRoot, objectInfo);
Vernon Mauery6c44a942023-07-27 11:00:10 -0700855 if (ec.value())
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600856 {
George Liu24fffdc2024-07-17 17:40:53 +0800857 lg2::error("Failed to locate System UUID object, "
858 "interface: {INTERFACE}, error: {ERROR}",
859 "INTERFACE", uuidInterface, "ERROR", ec.message());
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600860 }
Vernon Mauery6c44a942023-07-27 11:00:10 -0700861
862 // Read UUID property value from bmcObject
863 // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
864 std::string rfc4122Uuid{};
865 ec = ipmi::getDbusProperty(ctx, objectInfo.second, objectInfo.first,
866 uuidInterface, uuidProperty, rfc4122Uuid);
867 if (ec.value())
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600868 {
George Liu24fffdc2024-07-17 17:40:53 +0800869 lg2::error("Failed to read System UUID property, "
870 "interface: {INTERFACE}, property: {PROPERTY}, "
871 "error: {ERROR}",
872 "INTERFACE", uuidInterface, "PROPERTY", uuidProperty,
873 "ERROR", ec.message());
Vernon Maueryb90a5322019-03-25 13:36:55 -0700874 return ipmi::responseUnspecifiedError();
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600875 }
Vernon Maueryb90a5322019-03-25 13:36:55 -0700876 std::array<uint8_t, 16> uuid;
Vernon Maueryb90a5322019-03-25 13:36:55 -0700877 try
878 {
879 // convert to IPMI format
880 uuid = rfc4122ToIpmi(rfc4122Uuid);
881 }
882 catch (const InvalidArgument& e)
883 {
George Liu24fffdc2024-07-17 17:40:53 +0800884 lg2::error("Failed in parsing BMC UUID property, "
885 "interface: {INTERFACE}, property: {PROPERTY}, "
886 "value: {VALUE}, error: {ERROR}",
887 "INTERFACE", uuidInterface, "PROPERTY", uuidProperty,
888 "VALUE", rfc4122Uuid, "ERROR", e);
Vernon Maueryb90a5322019-03-25 13:36:55 -0700889 return ipmi::responseUnspecifiedError();
890 }
891 return ipmi::responseSuccess(uuid);
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600892}
893
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000894/**
895 * @brief set the session state as teardown
896 *
897 * This function is to set the session state to tear down in progress if the
898 * state is active.
899 *
900 * @param[in] busp - Dbus obj
901 * @param[in] service - service name
902 * @param[in] obj - object path
903 *
904 * @return success completion code if it sets the session state to
905 * tearDownInProgress else return the corresponding error completion code.
906 **/
907uint8_t setSessionState(std::shared_ptr<sdbusplus::asio::connection>& busp,
908 const std::string& service, const std::string& obj)
909{
910 try
911 {
912 uint8_t sessionState = std::get<uint8_t>(ipmi::getDbusProperty(
913 *busp, service, obj, session::sessionIntf, "State"));
914
915 if (sessionState == static_cast<uint8_t>(session::State::active))
916 {
917 ipmi::setDbusProperty(
918 *busp, service, obj, session::sessionIntf, "State",
919 static_cast<uint8_t>(session::State::tearDownInProgress));
920 return ipmi::ccSuccess;
921 }
922 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500923 catch (const std::exception& e)
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000924 {
George Liu24fffdc2024-07-17 17:40:53 +0800925 lg2::error("Failed in getting session state property, "
926 "service: {SERVICE}, object path: {OBJECT_PATH}, "
927 "interface: {INTERFACE}, error: {ERROR}",
928 "SERVICE", service, "OBJECT_PATH", obj, "INTERFACE",
929 session::sessionIntf, "ERROR", e);
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000930 return ipmi::ccUnspecifiedError;
931 }
932
933 return ipmi::ccInvalidFieldRequest;
934}
935
936ipmi::RspType<> ipmiAppCloseSession(uint32_t reqSessionId,
937 std::optional<uint8_t> requestSessionHandle)
938{
939 auto busp = getSdBus();
940 uint8_t reqSessionHandle =
941 requestSessionHandle.value_or(session::defaultSessionHandle);
942
943 if (reqSessionId == session::sessionZero &&
944 reqSessionHandle == session::defaultSessionHandle)
945 {
946 return ipmi::response(session::ccInvalidSessionId);
947 }
948
949 if (reqSessionId == session::sessionZero &&
950 reqSessionHandle == session::invalidSessionHandle)
951 {
952 return ipmi::response(session::ccInvalidSessionHandle);
953 }
954
955 if (reqSessionId != session::sessionZero &&
956 reqSessionHandle != session::defaultSessionHandle)
957 {
958 return ipmi::response(ipmi::ccInvalidFieldRequest);
959 }
960
961 try
962 {
963 ipmi::ObjectTree objectTree = ipmi::getAllDbusObjects(
964 *busp, session::sessionManagerRootPath, session::sessionIntf);
965
966 for (auto& objectTreeItr : objectTree)
967 {
968 const std::string obj = objectTreeItr.first;
969
970 if (isSessionObjectMatched(obj, reqSessionId, reqSessionHandle))
971 {
972 auto& serviceMap = objectTreeItr.second;
973
974 // Session id and session handle are unique for each session.
975 // Session id and handler are retrived from the object path and
976 // object path will be unique for each session. Checking if
977 // multiple objects exist with same object path under multiple
978 // services.
979 if (serviceMap.size() != 1)
980 {
981 return ipmi::responseUnspecifiedError();
982 }
983
984 auto itr = serviceMap.begin();
985 const std::string service = itr->first;
986 return ipmi::response(setSessionState(busp, service, obj));
987 }
988 }
989 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500990 catch (const sdbusplus::exception_t& e)
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000991 {
George Liu24fffdc2024-07-17 17:40:53 +0800992 lg2::error("Failed to fetch object from dbus, "
993 "interface: {INTERFACE}, error: {ERROR}",
994 "INTERFACE", session::sessionIntf, "ERROR", e);
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000995 return ipmi::responseUnspecifiedError();
996 }
997
998 return ipmi::responseInvalidFieldRequest();
999}
1000
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001001uint8_t getTotalSessionCount()
1002{
Meera-Kattac1789482021-05-18 09:53:26 +00001003 uint8_t count = 0, ch = 0;
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001004
1005 while (ch < ipmi::maxIpmiChannels &&
1006 count < session::maxNetworkInstanceSupported)
1007 {
Jayaprakash Mutyala80207e62020-07-04 16:34:15 +00001008 ipmi::ChannelInfo chInfo{};
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001009 ipmi::getChannelInfo(ch, chInfo);
1010 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) ==
1011 ipmi::EChannelMediumType::lan8032)
1012 {
1013 count++;
1014 }
1015 ch++;
1016 }
1017 return count * session::maxSessionCountPerChannel;
1018}
1019
1020/**
1021 * @brief get session info request data.
1022 *
1023 * This function validates the request data and retrive request session id,
1024 * session handle.
1025 *
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301026 * @param[in] ctx - context of current session.
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001027 * @param[in] sessionIndex - request session index
1028 * @param[in] payload - input payload
1029 * @param[in] reqSessionId - unpacked session Id will be asigned
1030 * @param[in] reqSessionHandle - unpacked session handle will be asigned
1031 *
1032 * @return success completion code if request data is valid
1033 * else return the correcponding error completion code.
1034 **/
Patrick Williams1318a5e2024-08-16 15:19:54 -04001035uint8_t getSessionInfoRequestData(
1036 const ipmi::Context::ptr ctx, const uint8_t sessionIndex,
1037 ipmi::message::Payload& payload, uint32_t& reqSessionId,
1038 uint8_t& reqSessionHandle)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001039{
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301040 if ((sessionIndex > session::maxSessionCountPerChannel) &&
1041 (sessionIndex < session::searchSessionByHandle))
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001042 {
1043 return ipmi::ccInvalidFieldRequest;
1044 }
1045
1046 switch (sessionIndex)
1047 {
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301048 case session::searchCurrentSession:
1049
1050 ipmi::ChannelInfo chInfo;
1051 ipmi::getChannelInfo(ctx->channel, chInfo);
1052
1053 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) !=
1054 ipmi::EChannelMediumType::lan8032)
1055 {
1056 return ipmi::ccInvalidFieldRequest;
1057 }
1058
1059 if (!payload.fullyUnpacked())
1060 {
1061 return ipmi::ccReqDataLenInvalid;
1062 }
1063 // Check if current sessionId is 0, sessionId 0 is reserved.
1064 if (ctx->sessionId == session::sessionZero)
1065 {
1066 return session::ccInvalidSessionId;
1067 }
1068 reqSessionId = ctx->sessionId;
1069 break;
1070
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001071 case session::searchSessionByHandle:
1072
1073 if ((payload.unpack(reqSessionHandle)) ||
1074 (!payload.fullyUnpacked()))
1075 {
1076 return ipmi::ccReqDataLenInvalid;
1077 }
1078
1079 if ((reqSessionHandle == session::sessionZero) ||
1080 ((reqSessionHandle & session::multiIntfaceSessionHandleMask) >
1081 session::maxSessionCountPerChannel))
1082 {
1083 return session::ccInvalidSessionHandle;
1084 }
1085 break;
1086
1087 case session::searchSessionById:
1088
1089 if ((payload.unpack(reqSessionId)) || (!payload.fullyUnpacked()))
1090 {
1091 return ipmi::ccReqDataLenInvalid;
1092 }
1093
1094 if (reqSessionId == session::sessionZero)
1095 {
1096 return session::ccInvalidSessionId;
1097 }
1098 break;
1099
1100 default:
1101 if (!payload.fullyUnpacked())
1102 {
1103 return ipmi::ccReqDataLenInvalid;
1104 }
1105 break;
1106 }
1107 return ipmi::ccSuccess;
1108}
1109
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001110uint8_t getSessionState(ipmi::Context::ptr ctx, const std::string& service,
1111 const std::string& objPath, uint8_t& sessionState)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001112{
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001113 boost::system::error_code ec = ipmi::getDbusProperty(
1114 ctx, service, objPath, session::sessionIntf, "State", sessionState);
1115 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001116 {
George Liu24fffdc2024-07-17 17:40:53 +08001117 lg2::error("Failed to fetch state property, service: {SERVICE}, "
1118 "object path: {OBJECTPATH}, interface: {INTERFACE}, "
1119 "error: {ERROR}",
1120 "SERVICE", service, "OBJECTPATH", objPath, "INTERFACE",
1121 session::sessionIntf, "ERROR", ec.message());
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001122 return ipmi::ccUnspecifiedError;
1123 }
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001124 return ipmi::ccSuccess;
1125}
1126
1127static constexpr uint8_t macAddrLen = 6;
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001128/** Alias SessionDetails - contain the optional information about an
1129 * RMCP+ session.
1130 *
1131 * @param userID - uint6_t session user ID (0-63)
1132 * @param reserved - uint2_t reserved
1133 * @param privilege - uint4_t session privilege (0-5)
1134 * @param reserved - uint4_t reserved
1135 * @param channel - uint4_t session channel number
1136 * @param protocol - uint4_t session protocol
1137 * @param remoteIP - uint32_t remote IP address
1138 * @param macAddr - std::array<uint8_t, 6> mac address
1139 * @param port - uint16_t remote port
1140 */
1141using SessionDetails =
1142 std::tuple<uint2_t, uint6_t, uint4_t, uint4_t, uint4_t, uint4_t, uint32_t,
1143 std::array<uint8_t, macAddrLen>, uint16_t>;
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001144
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001145/** @brief get session details for a given session
1146 *
1147 * @param[in] ctx - ipmi::Context pointer for accessing D-Bus
1148 * @param[in] service - D-Bus service name to fetch details from
1149 * @param[in] objPath - D-Bus object path for session
1150 * @param[out] sessionHandle - return session handle for session
1151 * @param[out] sessionState - return session state for session
1152 * @param[out] details - return a SessionDetails tuple containing other
1153 * session info
1154 * @return - ipmi::Cc success or error code
1155 */
1156ipmi::Cc getSessionDetails(ipmi::Context::ptr ctx, const std::string& service,
1157 const std::string& objPath, uint8_t& sessionHandle,
1158 uint8_t& sessionState, SessionDetails& details)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001159{
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001160 ipmi::PropertyMap sessionProps;
1161 boost::system::error_code ec = ipmi::getAllDbusProperties(
1162 ctx, service, objPath, session::sessionIntf, sessionProps);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001163
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001164 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001165 {
George Liu24fffdc2024-07-17 17:40:53 +08001166 lg2::error("Failed to fetch state property, service: {SERVICE}, "
1167 "object path: {OBJECTPATH}, interface: {INTERFACE}, "
1168 "error: {ERROR}",
1169 "SERVICE", service, "OBJECTPATH", objPath, "INTERFACE",
1170 session::sessionIntf, "ERROR", ec.message());
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001171 return ipmi::ccUnspecifiedError;
1172 }
1173
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001174 sessionState = ipmi::mappedVariant<uint8_t>(
1175 sessionProps, "State", static_cast<uint8_t>(session::State::inactive));
1176 if (sessionState == static_cast<uint8_t>(session::State::active))
1177 {
Patrick Williams1318a5e2024-08-16 15:19:54 -04001178 sessionHandle =
1179 ipmi::mappedVariant<uint8_t>(sessionProps, "SessionHandle", 0);
1180 std::get<0>(details) =
1181 ipmi::mappedVariant<uint8_t>(sessionProps, "UserID", 0xff);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001182 // std::get<1>(details) = 0; // (default constructed to 0)
1183 std::get<2>(details) =
1184 ipmi::mappedVariant<uint8_t>(sessionProps, "CurrentPrivilege", 0);
1185 // std::get<3>(details) = 0; // (default constructed to 0)
Patrick Williams1318a5e2024-08-16 15:19:54 -04001186 std::get<4>(details) =
1187 ipmi::mappedVariant<uint8_t>(sessionProps, "ChannelNum", 0xff);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001188 constexpr uint4_t rmcpPlusProtocol = 1;
1189 std::get<5>(details) = rmcpPlusProtocol;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001190 std::get<6>(details) =
1191 ipmi::mappedVariant<uint32_t>(sessionProps, "RemoteIPAddr", 0);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001192 // std::get<7>(details) = {{0}}; // default constructed to all 0
Patrick Williams1318a5e2024-08-16 15:19:54 -04001193 std::get<8>(details) =
1194 ipmi::mappedVariant<uint16_t>(sessionProps, "RemotePort", 0);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001195 }
1196
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001197 return ipmi::ccSuccess;
1198}
1199
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001200ipmi::RspType<uint8_t, // session handle,
1201 uint8_t, // total session count
1202 uint8_t, // active session count
1203 std::optional<SessionDetails>>
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301204 ipmiAppGetSessionInfo(ipmi::Context::ptr ctx, uint8_t sessionIndex,
1205 ipmi::message::Payload& payload)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001206{
1207 uint32_t reqSessionId = 0;
1208 uint8_t reqSessionHandle = session::defaultSessionHandle;
1209 // initializing state to 0xff as 0 represents state as inactive.
1210 uint8_t state = 0xFF;
1211
1212 uint8_t completionCode = getSessionInfoRequestData(
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301213 ctx, sessionIndex, payload, reqSessionId, reqSessionHandle);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001214
1215 if (completionCode)
1216 {
1217 return ipmi::response(completionCode);
1218 }
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001219 ipmi::ObjectTree objectTree;
1220 boost::system::error_code ec = ipmi::getAllDbusObjects(
1221 ctx, session::sessionManagerRootPath, session::sessionIntf, objectTree);
1222 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001223 {
George Liu24fffdc2024-07-17 17:40:53 +08001224 lg2::error("Failed to fetch object from dbus, "
1225 "interface: {INTERFACE}, error: {ERROR}",
1226 "INTERFACE", session::sessionIntf, "ERROR", ec.message());
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001227 return ipmi::responseUnspecifiedError();
1228 }
1229
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001230 uint8_t totalSessionCount = getTotalSessionCount();
1231 uint8_t activeSessionCount = 0;
1232 uint8_t sessionHandle = session::defaultSessionHandle;
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001233 uint8_t activeSessionHandle = 0;
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001234 std::optional<SessionDetails> maybeDetails;
1235 uint8_t index = 0;
1236 for (auto& objectTreeItr : objectTree)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001237 {
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001238 uint32_t sessionId = 0;
1239 std::string objectPath = objectTreeItr.first;
1240
1241 if (!parseCloseSessionInputPayload(objectPath, sessionId,
1242 sessionHandle))
1243 {
1244 continue;
1245 }
1246 index++;
1247 auto& serviceMap = objectTreeItr.second;
1248 auto itr = serviceMap.begin();
1249
1250 if (serviceMap.size() != 1)
1251 {
1252 return ipmi::responseUnspecifiedError();
1253 }
1254
1255 std::string service = itr->first;
1256 uint8_t sessionState = 0;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001257 completionCode =
1258 getSessionState(ctx, service, objectPath, sessionState);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001259 if (completionCode)
1260 {
1261 return ipmi::response(completionCode);
1262 }
1263
1264 if (sessionState == static_cast<uint8_t>(session::State::active))
1265 {
1266 activeSessionCount++;
1267 }
1268
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001269 if (index == sessionIndex || reqSessionId == sessionId ||
1270 reqSessionHandle == sessionHandle)
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001271 {
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001272 SessionDetails details{};
1273 completionCode = getSessionDetails(ctx, service, objectPath,
1274 sessionHandle, state, details);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001275
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001276 if (completionCode)
1277 {
1278 return ipmi::response(completionCode);
1279 }
1280 activeSessionHandle = sessionHandle;
1281 maybeDetails = std::move(details);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001282 }
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001283 }
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001284
1285 if (state == static_cast<uint8_t>(session::State::active) ||
1286 state == static_cast<uint8_t>(session::State::tearDownInProgress))
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001287 {
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001288 return ipmi::responseSuccess(activeSessionHandle, totalSessionCount,
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001289 activeSessionCount, maybeDetails);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001290 }
1291
1292 return ipmi::responseInvalidFieldRequest();
1293}
1294
Xo Wangf542e8b2017-08-09 15:34:16 -07001295static std::unique_ptr<SysInfoParamStore> sysInfoParamStore;
1296
Xo Wang87651332017-08-11 10:17:59 -07001297static std::string sysInfoReadSystemName()
1298{
1299 // Use the BMC hostname as the "System Name."
1300 char hostname[HOST_NAME_MAX + 1] = {};
1301 if (gethostname(hostname, HOST_NAME_MAX) != 0)
1302 {
1303 perror("System info parameter: system name");
1304 }
1305 return hostname;
1306}
1307
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001308static constexpr uint8_t paramRevision = 0x11;
1309static constexpr size_t configParameterLength = 16;
Xo Wangf542e8b2017-08-09 15:34:16 -07001310
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001311static constexpr size_t smallChunkSize = 14;
1312static constexpr size_t fullChunkSize = 16;
Jia, chunhui449f2162019-09-11 16:51:51 +08001313static constexpr uint8_t progressMask = 0x3;
krishnar410752832023-11-06 13:58:35 +05301314static constexpr uint8_t maxValidEncodingData = 0x02;
Xo Wangf542e8b2017-08-09 15:34:16 -07001315
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001316static constexpr uint8_t setComplete = 0x0;
1317static constexpr uint8_t setInProgress = 0x1;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001318static uint8_t transferStatus = setComplete;
1319
Jia, chunhui449f2162019-09-11 16:51:51 +08001320static constexpr uint8_t configDataOverhead = 2;
1321
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001322namespace ipmi
1323{
1324constexpr Cc ccParmNotSupported = 0x80;
Jia, chunhui449f2162019-09-11 16:51:51 +08001325constexpr Cc ccSetInProgressActive = 0x81;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001326
1327static inline auto responseParmNotSupported()
1328{
1329 return response(ccParmNotSupported);
Xo Wangf542e8b2017-08-09 15:34:16 -07001330}
Jia, chunhui449f2162019-09-11 16:51:51 +08001331static inline auto responseSetInProgressActive()
1332{
1333 return response(ccSetInProgressActive);
1334}
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001335} // namespace ipmi
Xo Wangf542e8b2017-08-09 15:34:16 -07001336
Jia, chunhui449f2162019-09-11 16:51:51 +08001337ipmi::RspType<uint8_t, // Parameter revision
1338 std::optional<uint8_t>, // data1 / setSelector / ProgressStatus
1339 std::optional<std::vector<uint8_t>>> // data2-17
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001340 ipmiAppGetSystemInfo(uint7_t reserved, bool getRevision,
1341 uint8_t paramSelector, uint8_t setSelector,
1342 uint8_t BlockSelector)
Xo Wangf542e8b2017-08-09 15:34:16 -07001343{
Snehalatha Va5ae7722020-05-02 18:18:57 +00001344 if (reserved || (paramSelector >= invalidParamSelectorStart &&
1345 paramSelector <= invalidParamSelectorEnd))
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001346 {
1347 return ipmi::responseInvalidFieldRequest();
1348 }
PavanKumarIntel3771f5f2023-11-02 06:26:42 +00001349 if (paramSelector >= oemCmdStart)
Snehalatha Va5ae7722020-05-02 18:18:57 +00001350 {
1351 return ipmi::responseParmNotSupported();
1352 }
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001353 if (getRevision)
Xo Wangf542e8b2017-08-09 15:34:16 -07001354 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001355 return ipmi::responseSuccess(paramRevision, std::nullopt, std::nullopt);
Xo Wangf542e8b2017-08-09 15:34:16 -07001356 }
1357
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001358 if (paramSelector == 0)
Xo Wangf542e8b2017-08-09 15:34:16 -07001359 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001360 return ipmi::responseSuccess(paramRevision, transferStatus,
1361 std::nullopt);
Xo Wangf542e8b2017-08-09 15:34:16 -07001362 }
1363
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001364 if (BlockSelector != 0) // 00h if parameter does not require a block number
Xo Wangf542e8b2017-08-09 15:34:16 -07001365 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001366 return ipmi::responseParmNotSupported();
Xo Wangf542e8b2017-08-09 15:34:16 -07001367 }
1368
1369 if (sysInfoParamStore == nullptr)
1370 {
1371 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
Xo Wang87651332017-08-11 10:17:59 -07001372 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1373 sysInfoReadSystemName);
Xo Wangf542e8b2017-08-09 15:34:16 -07001374 }
1375
1376 // Parameters other than Set In Progress are assumed to be strings.
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001377 std::tuple<bool, std::string> ret =
1378 sysInfoParamStore->lookup(paramSelector);
1379 bool found = std::get<0>(ret);
Xo Wangf542e8b2017-08-09 15:34:16 -07001380 if (!found)
1381 {
Snehalatha Va5ae7722020-05-02 18:18:57 +00001382 return ipmi::responseSensorInvalid();
Xo Wangf542e8b2017-08-09 15:34:16 -07001383 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001384 std::string& paramString = std::get<1>(ret);
Jia, chunhui449f2162019-09-11 16:51:51 +08001385 std::vector<uint8_t> configData;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001386 size_t count = 0;
1387 if (setSelector == 0)
Jia, chunhui449f2162019-09-11 16:51:51 +08001388 { // First chunk has only 14 bytes.
1389 configData.emplace_back(0); // encoding
1390 configData.emplace_back(paramString.length()); // string length
1391 count = std::min(paramString.length(), smallChunkSize);
1392 configData.resize(count + configDataOverhead);
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001393 std::copy_n(paramString.begin(), count,
Snehalatha Va5ae7722020-05-02 18:18:57 +00001394 configData.begin() + configDataOverhead); // 14 bytes chunk
1395
1396 // Append zero's to remaining bytes
1397 if (configData.size() < configParameterLength)
1398 {
1399 std::fill_n(std::back_inserter(configData),
1400 configParameterLength - configData.size(), 0x00);
1401 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001402 }
1403 else
Xo Wangf542e8b2017-08-09 15:34:16 -07001404 {
Jia, chunhui449f2162019-09-11 16:51:51 +08001405 size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001406 if (offset >= paramString.length())
1407 {
1408 return ipmi::responseParmOutOfRange();
1409 }
Jia, chunhui449f2162019-09-11 16:51:51 +08001410 count = std::min(paramString.length() - offset, fullChunkSize);
1411 configData.resize(count);
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001412 std::copy_n(paramString.begin() + offset, count,
1413 configData.begin()); // 16 bytes chunk
Xo Wangf542e8b2017-08-09 15:34:16 -07001414 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001415 return ipmi::responseSuccess(paramRevision, setSelector, configData);
Xo Wangf542e8b2017-08-09 15:34:16 -07001416}
1417
Jia, chunhui449f2162019-09-11 16:51:51 +08001418ipmi::RspType<> ipmiAppSetSystemInfo(uint8_t paramSelector, uint8_t data1,
1419 std::vector<uint8_t> configData)
1420{
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001421 if (paramSelector >= invalidParamSelectorStart &&
1422 paramSelector <= invalidParamSelectorEnd)
1423 {
1424 return ipmi::responseInvalidFieldRequest();
1425 }
PavanKumarIntel3771f5f2023-11-02 06:26:42 +00001426 if (paramSelector >= oemCmdStart)
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001427 {
1428 return ipmi::responseParmNotSupported();
1429 }
1430
Jia, chunhui449f2162019-09-11 16:51:51 +08001431 if (paramSelector == 0)
1432 {
1433 // attempt to set the 'set in progress' value (in parameter #0)
1434 // when not in the set complete state.
1435 if ((transferStatus != setComplete) && (data1 == setInProgress))
1436 {
1437 return ipmi::responseSetInProgressActive();
1438 }
1439 // only following 2 states are supported
1440 if (data1 > setInProgress)
1441 {
George Liu24fffdc2024-07-17 17:40:53 +08001442 lg2::error("illegal SetInProgress status");
Jia, chunhui449f2162019-09-11 16:51:51 +08001443 return ipmi::responseInvalidFieldRequest();
1444 }
1445
1446 transferStatus = data1 & progressMask;
1447 return ipmi::responseSuccess();
1448 }
1449
1450 if (configData.size() > configParameterLength)
1451 {
1452 return ipmi::responseInvalidFieldRequest();
1453 }
1454
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001455 // Append zero's to remaining bytes
1456 if (configData.size() < configParameterLength)
1457 {
1458 fill_n(back_inserter(configData),
1459 (configParameterLength - configData.size()), 0x00);
1460 }
1461
Jia, chunhui449f2162019-09-11 16:51:51 +08001462 if (!sysInfoParamStore)
1463 {
1464 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
1465 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1466 sysInfoReadSystemName);
1467 }
1468
1469 // lookup
1470 std::tuple<bool, std::string> ret =
1471 sysInfoParamStore->lookup(paramSelector);
1472 bool found = std::get<0>(ret);
1473 std::string& paramString = std::get<1>(ret);
1474 if (!found)
1475 {
1476 // parameter does not exist. Init new
1477 paramString = "";
1478 }
1479
1480 uint8_t setSelector = data1;
1481 size_t count = 0;
krishnar410752832023-11-06 13:58:35 +05301482 if (setSelector == 0) // First chunk has only 14 bytes.
Jia, chunhui449f2162019-09-11 16:51:51 +08001483 {
krishnar410752832023-11-06 13:58:35 +05301484 uint8_t encoding = configData.at(0);
1485 if (encoding > maxValidEncodingData)
1486 {
1487 return ipmi::responseInvalidFieldRequest();
1488 }
1489
Jia, chunhui449f2162019-09-11 16:51:51 +08001490 size_t stringLen = configData.at(1); // string length
Jia, chunhui449f2162019-09-11 16:51:51 +08001491 count = std::min(stringLen, smallChunkSize);
1492 count = std::min(count, configData.size());
1493 paramString.resize(stringLen); // reserve space
1494 std::copy_n(configData.begin() + configDataOverhead, count,
1495 paramString.begin());
1496 }
1497 else
1498 {
1499 size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
1500 if (offset >= paramString.length())
1501 {
1502 return ipmi::responseParmOutOfRange();
1503 }
1504 count = std::min(paramString.length() - offset, configData.size());
1505 std::copy_n(configData.begin(), count, paramString.begin() + offset);
1506 }
1507 sysInfoParamStore->update(paramSelector, paramString);
1508 return ipmi::responseSuccess();
1509}
1510
Yong Libd0503a2019-08-22 17:17:17 +08001511#ifdef ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301512inline std::vector<uint8_t> convertStringToData(const std::string& command)
1513{
1514 std::istringstream iss(command);
1515 std::string token;
1516 std::vector<uint8_t> dataValue;
1517 while (std::getline(iss, token, ' '))
1518 {
1519 dataValue.emplace_back(
1520 static_cast<uint8_t>(std::stoul(token, nullptr, base_16)));
1521 }
1522 return dataValue;
1523}
1524
Matt Simmering68d9d402023-11-09 14:22:11 -08001525static bool populateI2CControllerWRAllowlist()
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301526{
1527 nlohmann::json data = nullptr;
Matt Simmering68d9d402023-11-09 14:22:11 -08001528 std::ifstream jsonFile(i2cControllerWRAllowlistFile);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301529
1530 if (!jsonFile.good())
1531 {
George Liu24fffdc2024-07-17 17:40:53 +08001532 lg2::warning("i2c allow list file not found! file name: {FILE_NAME}",
1533 "FILE_NAME", i2cControllerWRAllowlistFile);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301534 return false;
1535 }
1536
1537 try
1538 {
1539 data = nlohmann::json::parse(jsonFile, nullptr, false);
1540 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001541 catch (const nlohmann::json::parse_error& e)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301542 {
George Liu24fffdc2024-07-17 17:40:53 +08001543 lg2::error("Corrupted i2c allow list config file, "
1544 "file name: {FILE_NAME}, error: {ERROR}",
1545 "FILE_NAME", i2cControllerWRAllowlistFile, "ERROR", e);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301546 return false;
1547 }
1548
1549 try
1550 {
1551 // Example JSON Structure format
1552 // "filters": [
1553 // {
1554 // "Description": "Allow full read - ignore first byte write value
1555 // for 0x40 to 0x4F",
1556 // "busId": "0x01",
1557 // "slaveAddr": "0x40",
1558 // "slaveAddrMask": "0x0F",
1559 // "command": "0x00",
1560 // "commandMask": "0xFF"
1561 // },
1562 // {
1563 // "Description": "Allow full read - first byte match 0x05 and
1564 // ignore second byte",
1565 // "busId": "0x01",
1566 // "slaveAddr": "0x57",
1567 // "slaveAddrMask": "0x00",
1568 // "command": "0x05 0x00",
1569 // "commandMask": "0x00 0xFF"
1570 // },]
1571
1572 nlohmann::json filters = data[filtersStr].get<nlohmann::json>();
Matt Simmering68d9d402023-11-09 14:22:11 -08001573 std::vector<i2cControllerWRAllowlist>& allowlist = getWRAllowlist();
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301574 for (const auto& it : filters.items())
1575 {
1576 nlohmann::json filter = it.value();
1577 if (filter.is_null())
1578 {
George Liu24fffdc2024-07-17 17:40:53 +08001579 lg2::error(
1580 "Corrupted I2C controller write read allowlist config file, "
1581 "file name: {FILE_NAME}",
1582 "FILE_NAME", i2cControllerWRAllowlistFile);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301583 return false;
1584 }
1585 const std::vector<uint8_t>& writeData =
1586 convertStringToData(filter[cmdStr].get<std::string>());
1587 const std::vector<uint8_t>& writeDataMask =
1588 convertStringToData(filter[cmdMaskStr].get<std::string>());
1589 if (writeDataMask.size() != writeData.size())
1590 {
George Liu24fffdc2024-07-17 17:40:53 +08001591 lg2::error("I2C controller write read allowlist filter "
1592 "mismatch for command & mask size");
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301593 return false;
1594 }
Matt Simmering68d9d402023-11-09 14:22:11 -08001595 allowlist.push_back(
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301596 {static_cast<uint8_t>(std::stoul(
1597 filter[busIdStr].get<std::string>(), nullptr, base_16)),
1598 static_cast<uint8_t>(
Matt Simmering68d9d402023-11-09 14:22:11 -08001599 std::stoul(filter[targetAddrStr].get<std::string>(),
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301600 nullptr, base_16)),
1601 static_cast<uint8_t>(
Matt Simmering68d9d402023-11-09 14:22:11 -08001602 std::stoul(filter[targetAddrMaskStr].get<std::string>(),
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301603 nullptr, base_16)),
1604 writeData, writeDataMask});
1605 }
Matt Simmering68d9d402023-11-09 14:22:11 -08001606 if (allowlist.size() != filters.size())
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301607 {
George Liu24fffdc2024-07-17 17:40:53 +08001608 lg2::error(
Matt Simmering68d9d402023-11-09 14:22:11 -08001609 "I2C controller write read allowlist filter size mismatch");
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301610 return false;
1611 }
1612 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001613 catch (const std::exception& e)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301614 {
George Liu24fffdc2024-07-17 17:40:53 +08001615 lg2::error("I2C controller write read allowlist "
1616 "unexpected exception: {ERROR}",
1617 "ERROR", e);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301618 return false;
1619 }
1620 return true;
1621}
1622
Matt Simmering68d9d402023-11-09 14:22:11 -08001623static inline bool isWriteDataAllowlisted(const std::vector<uint8_t>& data,
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301624 const std::vector<uint8_t>& dataMask,
1625 const std::vector<uint8_t>& writeData)
1626{
1627 std::vector<uint8_t> processedDataBuf(data.size());
1628 std::vector<uint8_t> processedReqBuf(dataMask.size());
1629 std::transform(writeData.begin(), writeData.end(), dataMask.begin(),
1630 processedReqBuf.begin(), std::bit_or<uint8_t>());
1631 std::transform(data.begin(), data.end(), dataMask.begin(),
1632 processedDataBuf.begin(), std::bit_or<uint8_t>());
1633
1634 return (processedDataBuf == processedReqBuf);
1635}
1636
Matt Simmering68d9d402023-11-09 14:22:11 -08001637static bool isCmdAllowlisted(uint8_t busId, uint8_t targetAddr,
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301638 std::vector<uint8_t>& writeData)
1639{
Matt Simmering68d9d402023-11-09 14:22:11 -08001640 std::vector<i2cControllerWRAllowlist>& allowList = getWRAllowlist();
1641 for (const auto& wlEntry : allowList)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301642 {
1643 if ((busId == wlEntry.busId) &&
Matt Simmering68d9d402023-11-09 14:22:11 -08001644 ((targetAddr | wlEntry.targetAddrMask) ==
1645 (wlEntry.targetAddr | wlEntry.targetAddrMask)))
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301646 {
1647 const std::vector<uint8_t>& dataMask = wlEntry.dataMask;
1648 // Skip as no-match, if requested write data is more than the
1649 // write data mask size
1650 if (writeData.size() > dataMask.size())
1651 {
1652 continue;
1653 }
Matt Simmering68d9d402023-11-09 14:22:11 -08001654 if (isWriteDataAllowlisted(wlEntry.data, dataMask, writeData))
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301655 {
1656 return true;
1657 }
1658 }
1659 }
1660 return false;
1661}
Yong Libd0503a2019-08-22 17:17:17 +08001662#else
Matt Simmering68d9d402023-11-09 14:22:11 -08001663static bool populateI2CControllerWRAllowlist()
Yong Libd0503a2019-08-22 17:17:17 +08001664{
George Liu24fffdc2024-07-17 17:40:53 +08001665 lg2::info("I2C_WHITELIST_CHECK is disabled, do not populate allowlist");
Yong Libd0503a2019-08-22 17:17:17 +08001666 return true;
1667}
1668#endif // ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301669
Matt Simmering68d9d402023-11-09 14:22:11 -08001670/** @brief implements controller write read IPMI command which can be used for
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301671 * low-level I2C/SMBus write, read or write-read access
1672 * @param isPrivateBus -to indicate private bus usage
1673 * @param busId - bus id
1674 * @param channelNum - channel number
1675 * @param reserved - skip 1 bit
Matt Simmering68d9d402023-11-09 14:22:11 -08001676 * @param targetAddr - target address
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301677 * @param read count - number of bytes to be read
1678 * @param writeData - data to be written
1679 *
1680 * @returns IPMI completion code plus response data
1681 * - readData - i2c response data
1682 */
Patrick Williams1318a5e2024-08-16 15:19:54 -04001683ipmi::RspType<std::vector<uint8_t>> ipmiControllerWriteRead(
1684 [[maybe_unused]] bool isPrivateBus, uint3_t busId,
1685 [[maybe_unused]] uint4_t channelNum, bool reserved, uint7_t targetAddr,
1686 uint8_t readCount, std::vector<uint8_t> writeData)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301687{
Jayaprakash Mutyalac2af98b2021-09-14 09:19:11 +00001688 if (reserved)
1689 {
1690 return ipmi::responseInvalidFieldRequest();
1691 }
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301692 const size_t writeCount = writeData.size();
1693 if (!readCount && !writeCount)
1694 {
George Liu24fffdc2024-07-17 17:40:53 +08001695 lg2::error("Controller write read command: Read & write count are 0");
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301696 return ipmi::responseInvalidFieldRequest();
1697 }
Yong Libd0503a2019-08-22 17:17:17 +08001698#ifdef ENABLE_I2C_WHITELIST_CHECK
Matt Simmering68d9d402023-11-09 14:22:11 -08001699 if (!isCmdAllowlisted(static_cast<uint8_t>(busId),
1700 static_cast<uint8_t>(targetAddr), writeData))
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301701 {
George Liu24fffdc2024-07-17 17:40:53 +08001702 lg2::error("Controller write read request blocked!, "
1703 "bus: {BUS}, addr: {ADDR}",
1704 "BUS", static_cast<uint8_t>(busId), "ADDR", lg2::hex,
1705 static_cast<uint8_t>(targetAddr));
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301706 }
Yong Libd0503a2019-08-22 17:17:17 +08001707#endif // ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301708 std::vector<uint8_t> readBuf(readCount);
Patrick Williams1318a5e2024-08-16 15:19:54 -04001709 std::string i2cBus =
1710 "/dev/i2c-" + std::to_string(static_cast<uint8_t>(busId));
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301711
Matt Simmering68d9d402023-11-09 14:22:11 -08001712 ipmi::Cc ret = ipmi::i2cWriteRead(i2cBus, static_cast<uint8_t>(targetAddr),
Yong Li7dc4ac02019-08-23 17:44:32 +08001713 writeData, readBuf);
1714 if (ret != ipmi::ccSuccess)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301715 {
Yong Li7dc4ac02019-08-23 17:44:32 +08001716 return ipmi::response(ret);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301717 }
1718 return ipmi::responseSuccess(readBuf);
1719}
1720
Chris Austen6caf28b2015-10-13 12:40:40 -05001721void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +05301722{
Vernon Mauery86a50822019-03-25 13:11:36 -07001723 // <Get Device ID>
1724 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1725 ipmi::app::cmdGetDeviceId, ipmi::Privilege::User,
1726 ipmiAppGetDeviceId);
1727
Tom05732372016-09-06 17:21:23 +05301728 // <Get BT Interface Capabilities>
Vernon Mauerycc99ba42019-03-25 13:40:11 -07001729 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1730 ipmi::app::cmdGetBtIfaceCapabilities,
1731 ipmi::Privilege::User, ipmiAppGetBtCapabilities);
Chris Austen6caf28b2015-10-13 12:40:40 -05001732
Tom05732372016-09-06 17:21:23 +05301733 // <Reset Watchdog Timer>
Vernon Mauery11df4f62019-03-25 14:17:54 -07001734 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1735 ipmi::app::cmdResetWatchdogTimer,
1736 ipmi::Privilege::Operator, ipmiAppResetWatchdogTimer);
Chris Austen6caf28b2015-10-13 12:40:40 -05001737
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001738 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301739 ipmi::app::cmdGetSessionInfo, ipmi::Privilege::User,
1740 ipmiAppGetSessionInfo);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001741
Tom05732372016-09-06 17:21:23 +05301742 // <Set Watchdog Timer>
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +00001743 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1744 ipmi::app::cmdSetWatchdogTimer,
1745 ipmi::Privilege::Operator, ipmiSetWatchdogTimer);
Chris Austen6caf28b2015-10-13 12:40:40 -05001746
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +00001747 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1748 ipmi::app::cmdCloseSession, ipmi::Privilege::Callback,
1749 ipmiAppCloseSession);
1750
William A. Kennington III73f44512018-02-09 15:28:46 -08001751 // <Get Watchdog Timer>
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +00001752 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301753 ipmi::app::cmdGetWatchdogTimer, ipmi::Privilege::User,
1754 ipmiGetWatchdogTimer);
William A. Kennington III73f44512018-02-09 15:28:46 -08001755
Tom05732372016-09-06 17:21:23 +05301756 // <Get Self Test Results>
Vernon Maueryb84a5282019-03-25 13:39:03 -07001757 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1758 ipmi::app::cmdGetSelfTestResults,
1759 ipmi::Privilege::User, ipmiAppGetSelfTestResults);
Nan Li41fa24a2016-11-10 20:12:37 +08001760
Tom05732372016-09-06 17:21:23 +05301761 // <Get Device GUID>
Vernon Mauery15541322019-03-25 13:33:03 -07001762 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1763 ipmi::app::cmdGetDeviceGuid, ipmi::Privilege::User,
1764 ipmiAppGetDeviceGuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -05001765
Tom05732372016-09-06 17:21:23 +05301766 // <Set ACPI Power State>
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +00001767 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1768 ipmi::app::cmdSetAcpiPowerState,
1769 ipmi::Privilege::Admin, ipmiSetAcpiPowerState);
Yong Li18d77262018-10-09 01:59:45 +08001770 // <Get ACPI Power State>
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +00001771 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1772 ipmi::app::cmdGetAcpiPowerState,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301773 ipmi::Privilege::User, ipmiGetAcpiPowerState);
Yong Li18d77262018-10-09 01:59:45 +08001774
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301775 // Note: For security reason, this command will be registered only when
Matt Simmering68d9d402023-11-09 14:22:11 -08001776 // there are proper I2C Controller write read allowlist
1777 if (populateI2CControllerWRAllowlist())
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301778 {
Matt Simmering68d9d402023-11-09 14:22:11 -08001779 // Note: For security reasons, registering controller write read as
1780 // admin privilege command, even though IPMI 2.0 specification allows it
1781 // as operator privilege.
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301782 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1783 ipmi::app::cmdMasterWriteRead,
Matt Simmering68d9d402023-11-09 14:22:11 -08001784 ipmi::Privilege::Admin, ipmiControllerWriteRead);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301785 }
1786
Marri Devender Rao5e007a52018-01-08 06:18:36 -06001787 // <Get System GUID Command>
Vernon Maueryb90a5322019-03-25 13:36:55 -07001788 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1789 ipmi::app::cmdGetSystemGuid, ipmi::Privilege::User,
1790 ipmiAppGetSystemGuid);
Tom Joseph7cbe2282018-03-21 21:17:33 +05301791
1792 // <Get Channel Cipher Suites Command>
Ayushi Smriti5c3b72c2019-08-30 13:47:31 +00001793 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1794 ipmi::app::cmdGetChannelCipherSuites,
Vernon Mauery79b4eea2019-11-07 09:51:39 -08001795 ipmi::Privilege::None, getChannelCipherSuites);
Vernon Maueryd2a57de2019-03-25 12:45:28 -07001796
Xo Wangf542e8b2017-08-09 15:34:16 -07001797 // <Get System Info Command>
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001798 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1799 ipmi::app::cmdGetSystemInfoParameters,
1800 ipmi::Privilege::User, ipmiAppGetSystemInfo);
Jia, chunhui449f2162019-09-11 16:51:51 +08001801 // <Set System Info Command>
1802 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1803 ipmi::app::cmdSetSystemInfoParameters,
1804 ipmi::Privilege::Admin, ipmiAppSetSystemInfo);
vishwabmcba0bd5f2015-09-30 16:50:23 +05301805 return;
1806}