blob: 85334fcb140402f098dec2d7a7308c8dd2ce5cd3 [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";
Thang Trana809fa52024-08-08 14:46:34 +070051constexpr auto versionPurposeHostEnd = ".Host";
Marri Devender Rao5e007a52018-01-08 06:18:36 -060052
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060053static constexpr auto redundancyIntf =
54 "xyz.openbmc_project.Software.RedundancyPriority";
Patrick Venture0b02be92018-08-31 11:55:55 -070055static constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060056static constexpr auto activationIntf =
57 "xyz.openbmc_project.Software.Activation";
58static constexpr auto softwareRoot = "/xyz/openbmc_project/software";
59
George Liu5087b072025-03-11 19:28:17 +080060void registerNetFnAppFunctions() __attribute__((constructor));
vishwabmcba0bd5f2015-09-30 16:50:23 +053061
Ratan Guptab8e99552017-07-27 07:07:48 +053062using namespace phosphor::logging;
Willy Tu523e2d12023-09-05 11:36:48 -070063using namespace sdbusplus::error::xyz::openbmc_project::common;
64using Version = sdbusplus::server::xyz::openbmc_project::software::Version;
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060065using Activation =
Willy Tu523e2d12023-09-05 11:36:48 -070066 sdbusplus::server::xyz::openbmc_project::software::Activation;
67using BMC = sdbusplus::server::xyz::openbmc_project::state::BMC;
Vernon Mauery185b9f82018-07-20 10:52:36 -070068namespace fs = std::filesystem;
Ratan Guptab8e99552017-07-27 07:07:48 +053069
Yong Libd0503a2019-08-22 17:17:17 +080070#ifdef ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053071typedef struct
72{
73 uint8_t busId;
Matt Simmering68d9d402023-11-09 14:22:11 -080074 uint8_t targetAddr;
75 uint8_t targetAddrMask;
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053076 std::vector<uint8_t> data;
77 std::vector<uint8_t> dataMask;
Matt Simmering68d9d402023-11-09 14:22:11 -080078} i2cControllerWRAllowlist;
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053079
Matt Simmering68d9d402023-11-09 14:22:11 -080080static std::vector<i2cControllerWRAllowlist>& getWRAllowlist()
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053081{
Matt Simmering68d9d402023-11-09 14:22:11 -080082 static std::vector<i2cControllerWRAllowlist> wrAllowlist;
83 return wrAllowlist;
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053084}
85
Matt Simmering68d9d402023-11-09 14:22:11 -080086static constexpr const char* i2cControllerWRAllowlistFile =
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053087 "/usr/share/ipmi-providers/master_write_read_white_list.json";
88
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053089static constexpr const char* filtersStr = "filters";
90static constexpr const char* busIdStr = "busId";
Matt Simmering68d9d402023-11-09 14:22:11 -080091static constexpr const char* targetAddrStr = "slaveAddr";
92static constexpr const char* targetAddrMaskStr = "slaveAddrMask";
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053093static constexpr const char* cmdStr = "command";
94static constexpr const char* cmdMaskStr = "commandMask";
95static constexpr int base_16 = 16;
Yong Libd0503a2019-08-22 17:17:17 +080096#endif // ENABLE_I2C_WHITELIST_CHECK
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +000097static constexpr uint8_t oemCmdStart = 192;
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +000098static constexpr uint8_t invalidParamSelectorStart = 8;
99static constexpr uint8_t invalidParamSelectorEnd = 191;
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +0530100
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600101/**
102 * @brief Returns the Version info from primary s/w object
103 *
104 * Get the Version info from the active s/w object which is having high
105 * "Priority" value(a smaller number is a higher priority) and "Purpose"
106 * is "BMC" from the list of all s/w objects those are implementing
107 * RedundancyPriority interface from the given softwareRoot path.
108 *
109 * @return On success returns the Version info from primary s/w object.
110 *
111 */
Vernon Maueryea1c4012019-05-24 13:26:16 -0700112std::string getActiveSoftwareVersionInfo(ipmi::Context::ptr ctx)
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600113{
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600114 std::string revision{};
Vernon Mauery86a50822019-03-25 13:11:36 -0700115 ipmi::ObjectTree objectTree;
116 try
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600117 {
Patrick Williams1318a5e2024-08-16 15:19:54 -0400118 objectTree =
119 ipmi::getAllDbusObjects(*ctx->bus, softwareRoot, redundancyIntf);
Vernon Mauery86a50822019-03-25 13:11:36 -0700120 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500121 catch (const sdbusplus::exception_t& e)
Vernon Mauery86a50822019-03-25 13:11:36 -0700122 {
George Liu24fffdc2024-07-17 17:40:53 +0800123 lg2::error("Failed to fetch redundancy object from dbus, "
124 "interface: {INTERFACE}, error: {ERROR}",
125 "INTERFACE", redundancyIntf, "ERROR", e);
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600126 elog<InternalFailure>();
127 }
128
129 auto objectFound = false;
130 for (auto& softObject : objectTree)
131 {
Patrick Williams1318a5e2024-08-16 15:19:54 -0400132 auto service =
133 ipmi::getService(*ctx->bus, redundancyIntf, softObject.first);
134 auto objValueTree =
135 ipmi::getManagedObjects(*ctx->bus, service, softwareRoot);
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600136
137 auto minPriority = 0xFF;
138 for (const auto& objIter : objValueTree)
139 {
140 try
141 {
142 auto& intfMap = objIter.second;
143 auto& redundancyPriorityProps = intfMap.at(redundancyIntf);
144 auto& versionProps = intfMap.at(versionIntf);
145 auto& activationProps = intfMap.at(activationIntf);
Vernon Maueryf442e112019-04-09 11:44:36 -0700146 auto priority =
147 std::get<uint8_t>(redundancyPriorityProps.at("Priority"));
William A. Kennington III4c008022018-10-12 17:18:14 -0700148 auto purpose =
Vernon Maueryf442e112019-04-09 11:44:36 -0700149 std::get<std::string>(versionProps.at("Purpose"));
150 auto activation =
151 std::get<std::string>(activationProps.at("Activation"));
William A. Kennington III4c008022018-10-12 17:18:14 -0700152 auto version =
Vernon Maueryf442e112019-04-09 11:44:36 -0700153 std::get<std::string>(versionProps.at("Version"));
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600154 if ((Version::convertVersionPurposeFromString(purpose) ==
155 Version::VersionPurpose::BMC) &&
156 (Activation::convertActivationsFromString(activation) ==
157 Activation::Activations::Active))
158 {
159 if (priority < minPriority)
160 {
161 minPriority = priority;
162 objectFound = true;
163 revision = std::move(version);
164 }
165 }
166 }
167 catch (const std::exception& e)
168 {
George Liu24fffdc2024-07-17 17:40:53 +0800169 lg2::error("error message: {ERROR}", "ERROR", e);
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600170 }
171 }
172 }
173
174 if (!objectFound)
175 {
George Liu24fffdc2024-07-17 17:40:53 +0800176 lg2::error("Could not found an BMC software Object");
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600177 elog<InternalFailure>();
178 }
179
180 return revision;
181}
182
Alexander Amelkinba19c182018-09-04 15:49:36 +0300183bool getCurrentBmcState()
184{
Patrick Williams5d82f472022-07-22 19:26:53 -0500185 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Alexander Amelkinba19c182018-09-04 15:49:36 +0300186
187 // Get the Inventory object implementing the BMC interface
Patrick Williams1318a5e2024-08-16 15:19:54 -0400188 ipmi::DbusObjectInfo bmcObject =
189 ipmi::getDbusObject(bus, bmc_state_interface);
190 auto variant =
191 ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first,
192 bmc_state_interface, bmc_state_property);
Alexander Amelkinba19c182018-09-04 15:49:36 +0300193
Vernon Maueryf442e112019-04-09 11:44:36 -0700194 return std::holds_alternative<std::string>(variant) &&
195 BMC::convertBMCStateFromString(std::get<std::string>(variant)) ==
196 BMC::BMCState::Ready;
Alexander Amelkinba19c182018-09-04 15:49:36 +0300197}
198
Patrick Venture94930a12019-04-30 10:01:58 -0700199bool getCurrentBmcStateWithFallback(const bool fallbackAvailability)
200{
201 try
202 {
203 return getCurrentBmcState();
204 }
205 catch (...)
206 {
207 // Nothing provided the BMC interface, therefore return whatever was
208 // configured as the default.
209 return fallbackAvailability;
210 }
211}
212
Yong Li18d77262018-10-09 01:59:45 +0800213namespace acpi_state
214{
Willy Tu523e2d12023-09-05 11:36:48 -0700215using namespace sdbusplus::server::xyz::openbmc_project::control::power;
Yong Li18d77262018-10-09 01:59:45 +0800216
Yong Li18d77262018-10-09 01:59:45 +0800217const static constexpr char* acpiInterface =
218 "xyz.openbmc_project.Control.Power.ACPIPowerState";
219const static constexpr char* sysACPIProp = "SysACPIStatus";
220const static constexpr char* devACPIProp = "DevACPIStatus";
221
222enum class PowerStateType : uint8_t
223{
224 sysPowerState = 0x00,
225 devPowerState = 0x01,
226};
227
228// Defined in 20.6 of ipmi doc
229enum class PowerState : uint8_t
230{
231 s0G0D0 = 0x00,
232 s1D1 = 0x01,
233 s2D2 = 0x02,
234 s3D3 = 0x03,
235 s4 = 0x04,
236 s5G2 = 0x05,
237 s4S5 = 0x06,
238 g3 = 0x07,
239 sleep = 0x08,
240 g1Sleep = 0x09,
241 override = 0x0a,
242 legacyOn = 0x20,
243 legacyOff = 0x21,
244 unknown = 0x2a,
245 noChange = 0x7f,
246};
247
248static constexpr uint8_t stateChanged = 0x80;
249
Yong Li18d77262018-10-09 01:59:45 +0800250std::map<ACPIPowerState::ACPI, PowerState> dbusToIPMI = {
251 {ACPIPowerState::ACPI::S0_G0_D0, PowerState::s0G0D0},
252 {ACPIPowerState::ACPI::S1_D1, PowerState::s1D1},
253 {ACPIPowerState::ACPI::S2_D2, PowerState::s2D2},
254 {ACPIPowerState::ACPI::S3_D3, PowerState::s3D3},
255 {ACPIPowerState::ACPI::S4, PowerState::s4},
256 {ACPIPowerState::ACPI::S5_G2, PowerState::s5G2},
257 {ACPIPowerState::ACPI::S4_S5, PowerState::s4S5},
258 {ACPIPowerState::ACPI::G3, PowerState::g3},
259 {ACPIPowerState::ACPI::SLEEP, PowerState::sleep},
260 {ACPIPowerState::ACPI::G1_SLEEP, PowerState::g1Sleep},
261 {ACPIPowerState::ACPI::OVERRIDE, PowerState::override},
262 {ACPIPowerState::ACPI::LEGACY_ON, PowerState::legacyOn},
263 {ACPIPowerState::ACPI::LEGACY_OFF, PowerState::legacyOff},
264 {ACPIPowerState::ACPI::Unknown, PowerState::unknown}};
265
266bool isValidACPIState(acpi_state::PowerStateType type, uint8_t state)
267{
268 if (type == acpi_state::PowerStateType::sysPowerState)
269 {
270 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::override)) ||
271 (state == static_cast<uint8_t>(acpi_state::PowerState::legacyOn)) ||
272 (state ==
273 static_cast<uint8_t>(acpi_state::PowerState::legacyOff)) ||
274 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
275 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
276 {
277 return true;
278 }
279 else
280 {
281 return false;
282 }
283 }
284 else if (type == acpi_state::PowerStateType::devPowerState)
285 {
286 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::s3D3)) ||
287 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
288 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
289 {
290 return true;
291 }
292 else
293 {
294 return false;
295 }
296 }
297 else
298 {
299 return false;
300 }
301 return false;
302}
303} // namespace acpi_state
304
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000305/** @brief implements Set ACPI Power State command
306 * @param sysAcpiState - ACPI system power state to set
307 * @param devAcpiState - ACPI device power state to set
308 *
309 * @return IPMI completion code on success
310 **/
311ipmi::RspType<> ipmiSetAcpiPowerState(uint8_t sysAcpiState,
312 uint8_t devAcpiState)
Chris Austen6caf28b2015-10-13 12:40:40 -0500313{
Yong Li18d77262018-10-09 01:59:45 +0800314 auto s = static_cast<uint8_t>(acpi_state::PowerState::unknown);
Yong Li18d77262018-10-09 01:59:45 +0800315
Patrick Williams5d82f472022-07-22 19:26:53 -0500316 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Yong Li18d77262018-10-09 01:59:45 +0800317
318 auto value = acpi_state::ACPIPowerState::ACPI::Unknown;
319
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000320 if (sysAcpiState & acpi_state::stateChanged)
Yong Li18d77262018-10-09 01:59:45 +0800321 {
322 // set system power state
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000323 s = sysAcpiState & ~acpi_state::stateChanged;
Yong Li18d77262018-10-09 01:59:45 +0800324
325 if (!acpi_state::isValidACPIState(
326 acpi_state::PowerStateType::sysPowerState, s))
327 {
George Liu24fffdc2024-07-17 17:40:53 +0800328 lg2::error("set_acpi_power sys invalid input, S: {S}", "S", s);
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000329 return ipmi::responseParmOutOfRange();
Yong Li18d77262018-10-09 01:59:45 +0800330 }
331
332 // valid input
333 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
334 {
George Liu24fffdc2024-07-17 17:40:53 +0800335 lg2::debug("No change for system power state");
Yong Li18d77262018-10-09 01:59:45 +0800336 }
337 else
338 {
Patrick Williams1318a5e2024-08-16 15:19:54 -0400339 auto found = std::find_if(
340 acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(),
341 [&s](const auto& iter) {
342 return (static_cast<uint8_t>(iter.second) == s);
343 });
Yong Li18d77262018-10-09 01:59:45 +0800344
345 value = found->first;
346
347 try
348 {
349 auto acpiObject =
350 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
351 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
352 acpi_state::acpiInterface,
353 acpi_state::sysACPIProp,
354 convertForMessage(value));
355 }
356 catch (const InternalFailure& e)
357 {
George Liu24fffdc2024-07-17 17:40:53 +0800358 lg2::error("Failed in set ACPI system property: {ERROR}",
359 "ERROR", e);
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000360 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800361 }
362 }
363 }
364 else
365 {
George Liu24fffdc2024-07-17 17:40:53 +0800366 lg2::debug("Do not change system power state");
Yong Li18d77262018-10-09 01:59:45 +0800367 }
368
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000369 if (devAcpiState & acpi_state::stateChanged)
Yong Li18d77262018-10-09 01:59:45 +0800370 {
371 // set device power state
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000372 s = devAcpiState & ~acpi_state::stateChanged;
Yong Li18d77262018-10-09 01:59:45 +0800373 if (!acpi_state::isValidACPIState(
374 acpi_state::PowerStateType::devPowerState, s))
375 {
George Liu24fffdc2024-07-17 17:40:53 +0800376 lg2::error("set_acpi_power dev invalid input, S: {S}", "S", s);
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000377 return ipmi::responseParmOutOfRange();
Yong Li18d77262018-10-09 01:59:45 +0800378 }
379
380 // valid input
381 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
382 {
George Liu24fffdc2024-07-17 17:40:53 +0800383 lg2::debug("No change for device power state");
Yong Li18d77262018-10-09 01:59:45 +0800384 }
385 else
386 {
Patrick Williams1318a5e2024-08-16 15:19:54 -0400387 auto found = std::find_if(
388 acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(),
389 [&s](const auto& iter) {
390 return (static_cast<uint8_t>(iter.second) == s);
391 });
Yong Li18d77262018-10-09 01:59:45 +0800392
393 value = found->first;
394
395 try
396 {
397 auto acpiObject =
398 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
399 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
400 acpi_state::acpiInterface,
401 acpi_state::devACPIProp,
402 convertForMessage(value));
403 }
404 catch (const InternalFailure& e)
405 {
George Liu24fffdc2024-07-17 17:40:53 +0800406 lg2::error("Failed in set ACPI device property: {ERROR}",
407 "ERROR", e);
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000408 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800409 }
410 }
411 }
412 else
413 {
George Liu24fffdc2024-07-17 17:40:53 +0800414 lg2::debug("Do not change device power state");
Yong Li18d77262018-10-09 01:59:45 +0800415 }
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000416 return ipmi::responseSuccess();
Yong Li18d77262018-10-09 01:59:45 +0800417}
418
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000419/**
420 * @brief implements the get ACPI power state command
421 *
422 * @return IPMI completion code plus response data on success.
423 * - ACPI system power state
424 * - ACPI device power state
425 **/
426ipmi::RspType<uint8_t, // acpiSystemPowerState
427 uint8_t // acpiDevicePowerState
428 >
429 ipmiGetAcpiPowerState()
Yong Li18d77262018-10-09 01:59:45 +0800430{
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000431 uint8_t sysAcpiState;
432 uint8_t devAcpiState;
Yong Li18d77262018-10-09 01:59:45 +0800433
Patrick Williams5d82f472022-07-22 19:26:53 -0500434 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Yong Li18d77262018-10-09 01:59:45 +0800435
Yong Li18d77262018-10-09 01:59:45 +0800436 try
437 {
438 auto acpiObject = ipmi::getDbusObject(bus, acpi_state::acpiInterface);
439
440 auto sysACPIVal = ipmi::getDbusProperty(
441 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
442 acpi_state::sysACPIProp);
443 auto sysACPI = acpi_state::ACPIPowerState::convertACPIFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -0700444 std::get<std::string>(sysACPIVal));
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000445 sysAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(sysACPI));
Yong Li18d77262018-10-09 01:59:45 +0800446
447 auto devACPIVal = ipmi::getDbusProperty(
448 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
449 acpi_state::devACPIProp);
450 auto devACPI = acpi_state::ACPIPowerState::convertACPIFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -0700451 std::get<std::string>(devACPIVal));
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000452 devAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(devACPI));
Yong Li18d77262018-10-09 01:59:45 +0800453 }
454 catch (const InternalFailure& e)
455 {
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000456 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800457 }
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000458
459 return ipmi::responseSuccess(sysAcpiState, devAcpiState);
Chris Austen6caf28b2015-10-13 12:40:40 -0500460}
461
Chris Austen7303bdc2016-04-17 11:50:54 -0500462typedef struct
463{
464 char major;
465 char minor;
Potin Laic7c55922023-02-16 10:33:55 +0800466 uint8_t aux[4];
Vernon Mauery86a50822019-03-25 13:11:36 -0700467} Revision;
Chris Austen7303bdc2016-04-17 11:50:54 -0500468
Potin Lai5d214202023-01-16 18:11:49 +0800469/* Use regular expression searching matched pattern X.Y, and convert it to */
470/* Major (X) and Minor (Y) version. */
471/* Example: */
472/* version = 2.14.0-dev */
473/* ^ ^ */
474/* | |---------------- Minor */
475/* |------------------ Major */
476/* */
Potin Laic7c55922023-02-16 10:33:55 +0800477/* Default regex string only tries to match Major and Minor version. */
478/* */
479/* To match more firmware version info, platforms need to define it own */
480/* regex string to match more strings, and assign correct mapping index in */
481/* matches array. */
482/* */
483/* matches[0]: matched index for major ver */
484/* matches[1]: matched index for minor ver */
485/* matches[2]: matched index for aux[0] (set 0 to skip) */
486/* matches[3]: matched index for aux[1] (set 0 to skip) */
487/* matches[4]: matched index for aux[2] (set 0 to skip) */
488/* matches[5]: matched index for aux[3] (set 0 to skip) */
489/* Example: */
490/* regex = "([\d]+).([\d]+).([\d]+)-dev-([\d]+)-g([0-9a-fA-F]{2}) */
491/* ([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})" */
492/* matches = {1,2,5,6,7,8} */
493/* version = 2.14.0-dev-750-g37a7c5ad1-dirty */
494/* ^ ^ ^ ^ ^ ^ ^ ^ */
495/* | | | | | | | | */
496/* | | | | | | | |-- Aux byte 3 (0xAD), index 8 */
497/* | | | | | | |---- Aux byte 2 (0xC5), index 7 */
498/* | | | | | |------ Aux byte 1 (0xA7), index 6 */
499/* | | | | |-------- Aux byte 0 (0x37), index 5 */
500/* | | | |------------- Not used, index 4 */
501/* | | |------------------- Not used, index 3 */
502/* | |---------------------- Minor (14), index 2 */
503/* |------------------------ Major (2), index 1 */
Potin Lai5d214202023-01-16 18:11:49 +0800504int convertVersion(std::string s, Revision& rev)
Chris Austen7303bdc2016-04-17 11:50:54 -0500505{
Potin Laic7c55922023-02-16 10:33:55 +0800506 static const std::vector<size_t> matches = {
507 MAJOR_MATCH_INDEX, MINOR_MATCH_INDEX, AUX_0_MATCH_INDEX,
508 AUX_1_MATCH_INDEX, AUX_2_MATCH_INDEX, AUX_3_MATCH_INDEX};
Potin Lai5d214202023-01-16 18:11:49 +0800509 std::regex fw_regex(FW_VER_REGEX);
510 std::smatch m;
511 Revision r = {0};
512 size_t val;
Chris Austen7303bdc2016-04-17 11:50:54 -0500513
Potin Laidcde04e2023-03-16 18:44:15 +0800514 if (std::regex_search(s, m, fw_regex))
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600515 {
Potin Laic7c55922023-02-16 10:33:55 +0800516 if (m.size() < *std::max_element(matches.begin(), matches.end()))
517 { // max index higher than match count
Potin Laidcde04e2023-03-16 18:44:15 +0800518 return -1;
519 }
520
Potin Lai5d214202023-01-16 18:11:49 +0800521 // convert major
522 {
Jayanth Othayotha6c307a2024-12-08 09:07:14 -0600523 std::string str = m[matches[0]].str();
524 const auto& [ptr, ec] =
525 std::from_chars(str.data(), str.data() + str.size(), val);
526 if (ec != std::errc() || ptr != str.data() + str.size())
Potin Lai5d214202023-01-16 18:11:49 +0800527 { // 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 {
Jayanth Othayotha6c307a2024-12-08 09:07:14 -0600546 std::string str = m[matches[1]].str();
547 const auto& [ptr, ec] =
548 std::from_chars(str.data(), str.data() + str.size(), val);
549 if (ec != std::errc() || ptr != str.data() + str.size())
Potin Lai5d214202023-01-16 18:11:49 +0800550 { // failed to convert minor string
Potin Laidcde04e2023-03-16 18:44:15 +0800551 return -1;
Potin Lai5d214202023-01-16 18:11:49 +0800552 }
553 r.minor = val & 0xFF;
554 }
555
Potin Laic7c55922023-02-16 10:33:55 +0800556 // convert aux bytes
557 {
558 size_t i;
559 for (i = 0; i < 4; i++)
560 {
561 if (matches[i + 2] == 0)
562 {
563 continue;
564 }
565
Jayanth Othayotha6c307a2024-12-08 09:07:14 -0600566 std::string str = m[matches[i + 2]].str();
567 const char* cstr = str.c_str();
Potin Laic7c55922023-02-16 10:33:55 +0800568 auto [ptr,
Jayanth Othayotha6c307a2024-12-08 09:07:14 -0600569 ec] = std::from_chars(cstr, cstr + str.size(), val, 16);
570 if (ec != std::errc() || ptr != cstr + str.size())
Potin Laic7c55922023-02-16 10:33:55 +0800571 { // failed to convert aux byte string
572 break;
573 }
574
575 r.aux[i] = val & 0xFF;
576 }
577
578 if (i != 4)
579 { // something wrong durign converting aux bytes
580 return -1;
581 }
582 }
583
Potin Lai5d214202023-01-16 18:11:49 +0800584 // all matched
585 rev = r;
586 return 0;
Chris Austen176c9652016-04-30 16:32:17 -0500587 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500588
Potin Lai5d214202023-01-16 18:11:49 +0800589 return -1;
Chris Austen7303bdc2016-04-17 11:50:54 -0500590}
591
Vernon Maueryea1c4012019-05-24 13:26:16 -0700592/* @brief: Implement the Get Device ID IPMI command per the IPMI spec
593 * @param[in] ctx - shared_ptr to an IPMI context struct
594 *
595 * @returns IPMI completion code plus response data
596 * - Device ID (manufacturer defined)
597 * - Device revision[4 bits]; reserved[3 bits]; SDR support[1 bit]
598 * - FW revision major[7 bits] (binary encoded); available[1 bit]
599 * - FW Revision minor (BCD encoded)
600 * - IPMI version (0x02 for IPMI 2.0)
601 * - device support (bitfield of supported options)
602 * - MFG IANA ID (3 bytes)
603 * - product ID (2 bytes)
604 * - AUX info (4 bytes)
605 */
606ipmi::RspType<uint8_t, // Device ID
607 uint8_t, // Device Revision
608 uint8_t, // Firmware Revision Major
609 uint8_t, // Firmware Revision minor
610 uint8_t, // IPMI version
611 uint8_t, // Additional device support
612 uint24_t, // MFG ID
613 uint16_t, // Product ID
614 uint32_t // AUX info
615 >
Willy Tu11d68892022-01-20 10:37:34 -0800616 ipmiAppGetDeviceId([[maybe_unused]] ipmi::Context::ptr ctx)
Chris Austen6caf28b2015-10-13 12:40:40 -0500617{
Vernon Mauery86a50822019-03-25 13:11:36 -0700618 static struct
619 {
620 uint8_t id;
621 uint8_t revision;
622 uint8_t fw[2];
623 uint8_t ipmiVer;
624 uint8_t addnDevSupport;
625 uint24_t manufId;
626 uint16_t prodId;
627 uint32_t aux;
628 } devId;
David Cobbleya1adb072017-11-21 15:58:13 -0800629 static bool dev_id_initialized = false;
Patrick Venture94930a12019-04-30 10:01:58 -0700630 static bool defaultActivationSetting = true;
David Cobbleya1adb072017-11-21 15:58:13 -0800631 const char* filename = "/usr/share/ipmi-providers/dev_id.json";
Alexander Amelkinba19c182018-09-04 15:49:36 +0300632 constexpr auto ipmiDevIdStateShift = 7;
633 constexpr auto ipmiDevIdFw1Mask = ~(1 << ipmiDevIdStateShift);
Willy Tub78184e2022-10-27 22:57:38 +0000634
635#ifdef GET_DBUS_ACTIVE_SOFTWARE
636 static bool haveBMCVersion = false;
JeffLin27a62ec2021-11-24 15:40:33 +0800637 if (!haveBMCVersion || !dev_id_initialized)
David Cobbleya1adb072017-11-21 15:58:13 -0800638 {
Willy Tub78184e2022-10-27 22:57:38 +0000639 int r = -1;
Jayanth Othayothceee8112024-12-08 09:46:37 -0600640 Revision rev = {0, 0, {0, 0, 0, 0}};
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600641 try
642 {
Vernon Maueryea1c4012019-05-24 13:26:16 -0700643 auto version = getActiveSoftwareVersionInfo(ctx);
Vernon Mauery86a50822019-03-25 13:11:36 -0700644 r = convertVersion(version, rev);
David Cobbleya1adb072017-11-21 15:58:13 -0800645 }
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600646 catch (const std::exception& e)
647 {
George Liu24fffdc2024-07-17 17:40:53 +0800648 lg2::error("error message: {ERROR}", "ERROR", e);
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600649 }
Nan Liee0cb902016-07-11 15:38:03 +0800650
Patrick Venture0b02be92018-08-31 11:55:55 -0700651 if (r >= 0)
652 {
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600653 // bit7 identifies if the device is available
654 // 0=normal operation
655 // 1=device firmware, SDR update,
656 // or self-initialization in progress.
Alexander Amelkinba19c182018-09-04 15:49:36 +0300657 // The availability may change in run time, so mask here
658 // and initialize later.
Vernon Mauery86a50822019-03-25 13:11:36 -0700659 devId.fw[0] = rev.major & ipmiDevIdFw1Mask;
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600660
661 rev.minor = (rev.minor > 99 ? 99 : rev.minor);
Vernon Mauery86a50822019-03-25 13:11:36 -0700662 devId.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16;
Potin Laic7c55922023-02-16 10:33:55 +0800663 std::memcpy(&devId.aux, rev.aux, sizeof(rev.aux));
Brandon Kimc1f5aca2022-03-17 17:54:06 -0700664 haveBMCVersion = true;
David Cobbleya1adb072017-11-21 15:58:13 -0800665 }
JeffLin27a62ec2021-11-24 15:40:33 +0800666 }
Willy Tub78184e2022-10-27 22:57:38 +0000667#endif
JeffLin27a62ec2021-11-24 15:40:33 +0800668 if (!dev_id_initialized)
669 {
David Cobbleya1adb072017-11-21 15:58:13 -0800670 // IPMI Spec version 2.0
Vernon Mauery86a50822019-03-25 13:11:36 -0700671 devId.ipmiVer = 2;
Adriana Kobylak0e912642016-06-22 16:54:39 -0500672
Vernon Mauery86a50822019-03-25 13:11:36 -0700673 std::ifstream devIdFile(filename);
674 if (devIdFile.is_open())
David Cobbleya1adb072017-11-21 15:58:13 -0800675 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700676 auto data = nlohmann::json::parse(devIdFile, nullptr, false);
David Cobbleya1adb072017-11-21 15:58:13 -0800677 if (!data.is_discarded())
678 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700679 devId.id = data.value("id", 0);
680 devId.revision = data.value("revision", 0);
681 devId.addnDevSupport = data.value("addn_dev_support", 0);
682 devId.manufId = data.value("manuf_id", 0);
683 devId.prodId = data.value("prod_id", 0);
Potin Lai9a686362023-09-05 09:27:53 +0800684#ifdef GET_DBUS_ACTIVE_SOFTWARE
685 if (!(AUX_0_MATCH_INDEX || AUX_1_MATCH_INDEX ||
686 AUX_2_MATCH_INDEX || AUX_3_MATCH_INDEX))
687#endif
688 {
689 devId.aux = data.value("aux", 0);
690 }
David Cobbleya1adb072017-11-21 15:58:13 -0800691
Willy Tubfd3a172022-05-31 13:57:54 -0700692 if (data.contains("firmware_revision"))
693 {
694 const auto& firmwareRevision = data.at("firmware_revision");
695 if (firmwareRevision.contains("major"))
696 {
697 firmwareRevision.at("major").get_to(devId.fw[0]);
698 }
699 if (firmwareRevision.contains("minor"))
700 {
701 firmwareRevision.at("minor").get_to(devId.fw[1]);
702 }
703 }
704
Patrick Venture94930a12019-04-30 10:01:58 -0700705 // Set the availablitity of the BMC.
706 defaultActivationSetting = data.value("availability", true);
707
Patrick Venture0b02be92018-08-31 11:55:55 -0700708 // Don't read the file every time if successful
David Cobbleya1adb072017-11-21 15:58:13 -0800709 dev_id_initialized = true;
710 }
711 else
712 {
George Liu24fffdc2024-07-17 17:40:53 +0800713 lg2::error("Device ID JSON parser failure");
Vernon Maueryf2587fc2019-04-09 14:28:15 -0700714 return ipmi::responseUnspecifiedError();
David Cobbleya1adb072017-11-21 15:58:13 -0800715 }
716 }
717 else
718 {
George Liu24fffdc2024-07-17 17:40:53 +0800719 lg2::error("Device ID file not found");
Vernon Maueryf2587fc2019-04-09 14:28:15 -0700720 return ipmi::responseUnspecifiedError();
Chris Austen7303bdc2016-04-17 11:50:54 -0500721 }
722 }
Chris Austen6caf28b2015-10-13 12:40:40 -0500723
Alexander Amelkinba19c182018-09-04 15:49:36 +0300724 // Set availability to the actual current BMC state
Vernon Mauery86a50822019-03-25 13:11:36 -0700725 devId.fw[0] &= ipmiDevIdFw1Mask;
Patrick Venture94930a12019-04-30 10:01:58 -0700726 if (!getCurrentBmcStateWithFallback(defaultActivationSetting))
Alexander Amelkinba19c182018-09-04 15:49:36 +0300727 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700728 devId.fw[0] |= (1 << ipmiDevIdStateShift);
Alexander Amelkinba19c182018-09-04 15:49:36 +0300729 }
730
Vernon Mauery86a50822019-03-25 13:11:36 -0700731 return ipmi::responseSuccess(
732 devId.id, devId.revision, devId.fw[0], devId.fw[1], devId.ipmiVer,
733 devId.addnDevSupport, devId.manufId, devId.prodId, devId.aux);
Chris Austen6caf28b2015-10-13 12:40:40 -0500734}
735
Vernon Maueryb84a5282019-03-25 13:39:03 -0700736auto ipmiAppGetSelfTestResults() -> ipmi::RspType<uint8_t, uint8_t>
Nan Li41fa24a2016-11-10 20:12:37 +0800737{
Nan Li41fa24a2016-11-10 20:12:37 +0800738 // Byte 2:
739 // 55h - No error.
Gunnar Mills8991dd62017-10-25 17:11:29 -0500740 // 56h - Self Test function not implemented in this controller.
Nan Li41fa24a2016-11-10 20:12:37 +0800741 // 57h - Corrupted or inaccesssible data or devices.
742 // 58h - Fatal hardware error.
743 // FFh - reserved.
744 // all other: Device-specific 'internal failure'.
745 // Byte 3:
746 // For byte 2 = 55h, 56h, FFh: 00h
747 // For byte 2 = 58h, all other: Device-specific
748 // For byte 2 = 57h: self-test error bitfield.
749 // Note: returning 57h does not imply that all test were run.
750 // [7] 1b = Cannot access SEL device.
751 // [6] 1b = Cannot access SDR Repository.
752 // [5] 1b = Cannot access BMC FRU device.
753 // [4] 1b = IPMB signal lines do not respond.
754 // [3] 1b = SDR Repository empty.
755 // [2] 1b = Internal Use Area of BMC FRU corrupted.
756 // [1] 1b = controller update 'boot block' firmware corrupted.
757 // [0] 1b = controller operational firmware corrupted.
Vernon Maueryb84a5282019-03-25 13:39:03 -0700758 constexpr uint8_t notImplemented = 0x56;
759 constexpr uint8_t zero = 0;
760 return ipmi::responseSuccess(notImplemented, zero);
Nan Li41fa24a2016-11-10 20:12:37 +0800761}
762
Vernon Mauery15541322019-03-25 13:33:03 -0700763static constexpr size_t uuidBinaryLength = 16;
764static std::array<uint8_t, uuidBinaryLength> rfc4122ToIpmi(std::string rfc4122)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500765{
Willy Tu523e2d12023-09-05 11:36:48 -0700766 using Argument = xyz::openbmc_project::common::InvalidArgument;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500767 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
Patrick Ventured2117022018-02-06 08:54:37 -0800768 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte
769 // order
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500770 // Ex: 0x2332fc2c40e66298e511f2782395a361
Vernon Mauery15541322019-03-25 13:33:03 -0700771 constexpr size_t uuidHexLength = (2 * uuidBinaryLength);
772 constexpr size_t uuidRfc4122Length = (uuidHexLength + 4);
773 std::array<uint8_t, uuidBinaryLength> uuid;
774 if (rfc4122.size() == uuidRfc4122Length)
Patrick Venture0b02be92018-08-31 11:55:55 -0700775 {
Vernon Mauery15541322019-03-25 13:33:03 -0700776 rfc4122.erase(std::remove(rfc4122.begin(), rfc4122.end(), '-'),
777 rfc4122.end());
Sergey Solomineb9b8142016-08-23 09:07:28 -0500778 }
Vernon Mauery15541322019-03-25 13:33:03 -0700779 if (rfc4122.size() != uuidHexLength)
vishwa1eaea4f2016-02-26 11:57:40 -0600780 {
Vernon Mauery15541322019-03-25 13:33:03 -0700781 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
782 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
vishwa1eaea4f2016-02-26 11:57:40 -0600783 }
Vernon Mauery15541322019-03-25 13:33:03 -0700784 for (size_t ind = 0; ind < uuidHexLength; ind += 2)
vishwa1eaea4f2016-02-26 11:57:40 -0600785 {
Vernon Mauery15541322019-03-25 13:33:03 -0700786 char v[3];
787 v[0] = rfc4122[ind];
788 v[1] = rfc4122[ind + 1];
789 v[2] = 0;
790 size_t err;
791 long b;
792 try
Emily Shafferedb8bb02018-09-27 14:50:15 -0700793 {
Vernon Mauery15541322019-03-25 13:33:03 -0700794 b = std::stoul(v, &err, 16);
Emily Shafferedb8bb02018-09-27 14:50:15 -0700795 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500796 catch (const std::exception& e)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500797 {
Vernon Mauery15541322019-03-25 13:33:03 -0700798 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
799 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500800 }
Vernon Mauery15541322019-03-25 13:33:03 -0700801 // check that exactly two ascii bytes were converted
802 if (err != 2)
803 {
804 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
805 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
806 }
807 uuid[uuidBinaryLength - (ind / 2) - 1] = static_cast<uint8_t>(b);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500808 }
Vernon Mauery15541322019-03-25 13:33:03 -0700809 return uuid;
810}
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500811
Vernon Mauery15541322019-03-25 13:33:03 -0700812auto ipmiAppGetDeviceGuid()
813 -> ipmi::RspType<std::array<uint8_t, uuidBinaryLength>>
814{
815 // return a fixed GUID based on /etc/machine-id
816 // This should match the /redfish/v1/Managers/bmc's UUID data
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500817
Vernon Mauery15541322019-03-25 13:33:03 -0700818 // machine specific application ID (for BMC ID)
819 // generated by systemd-id128 -p new as per man page
820 static constexpr sd_id128_t bmcUuidAppId = SD_ID128_MAKE(
821 e0, e1, 73, 76, 64, 61, 47, da, a5, 0c, d0, cc, 64, 12, 45, 78);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500822
Vernon Mauery15541322019-03-25 13:33:03 -0700823 sd_id128_t bmcUuid;
824 // create the UUID from /etc/machine-id via the systemd API
825 sd_id128_get_machine_app_specific(bmcUuidAppId, &bmcUuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500826
Vernon Mauery15541322019-03-25 13:33:03 -0700827 char bmcUuidCstr[SD_ID128_STRING_MAX];
828 std::string systemUuid = sd_id128_to_string(bmcUuid, bmcUuidCstr);
829
830 std::array<uint8_t, uuidBinaryLength> uuid = rfc4122ToIpmi(systemUuid);
831 return ipmi::responseSuccess(uuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500832}
Chris Austen6caf28b2015-10-13 12:40:40 -0500833
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700834auto ipmiAppGetBtCapabilities()
835 -> ipmi::RspType<uint8_t, uint8_t, uint8_t, uint8_t, uint8_t>
vishwabmcba0bd5f2015-09-30 16:50:23 +0530836{
Adriana Kobylak88ad8152016-12-13 10:09:08 -0600837 // Per IPMI 2.0 spec, the input and output buffer size must be the max
838 // buffer size minus one byte to allocate space for the length byte.
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700839 constexpr uint8_t nrOutstanding = 0x01;
840 constexpr uint8_t inputBufferSize = MAX_IPMI_BUFFER - 1;
841 constexpr uint8_t outputBufferSize = MAX_IPMI_BUFFER - 1;
842 constexpr uint8_t transactionTime = 0x0A;
843 constexpr uint8_t nrRetries = 0x01;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530844
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700845 return ipmi::responseSuccess(nrOutstanding, inputBufferSize,
846 outputBufferSize, transactionTime, nrRetries);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530847}
848
Vernon Mauery6c44a942023-07-27 11:00:10 -0700849auto ipmiAppGetSystemGuid(ipmi::Context::ptr& ctx)
850 -> ipmi::RspType<std::array<uint8_t, 16>>
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600851{
Prithvi Paid7966892025-08-02 18:40:13 +0530852 static constexpr auto uuidService = "xyz.openbmc_project.Settings";
853 static constexpr auto uuidObject = "/xyz/openbmc_project/Common/UUID";
Vernon Maueryb90a5322019-03-25 13:36:55 -0700854 static constexpr auto uuidInterface = "xyz.openbmc_project.Common.UUID";
855 static constexpr auto uuidProperty = "UUID";
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600856
Prithvi Paid7966892025-08-02 18:40:13 +0530857 // Read UUID property value from settings object
Vernon Mauery6c44a942023-07-27 11:00:10 -0700858 // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
859 std::string rfc4122Uuid{};
Prithvi Paid7966892025-08-02 18:40:13 +0530860 boost::system::error_code ec = ipmi::getDbusProperty(
861 ctx, uuidService, uuidObject, uuidInterface, uuidProperty, rfc4122Uuid);
Vernon Mauery6c44a942023-07-27 11:00:10 -0700862 if (ec.value())
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600863 {
George Liu24fffdc2024-07-17 17:40:53 +0800864 lg2::error("Failed to read System UUID property, "
865 "interface: {INTERFACE}, property: {PROPERTY}, "
866 "error: {ERROR}",
867 "INTERFACE", uuidInterface, "PROPERTY", uuidProperty,
868 "ERROR", ec.message());
Vernon Maueryb90a5322019-03-25 13:36:55 -0700869 return ipmi::responseUnspecifiedError();
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600870 }
Vernon Maueryb90a5322019-03-25 13:36:55 -0700871 std::array<uint8_t, 16> uuid;
Vernon Maueryb90a5322019-03-25 13:36:55 -0700872 try
873 {
874 // convert to IPMI format
875 uuid = rfc4122ToIpmi(rfc4122Uuid);
876 }
877 catch (const InvalidArgument& e)
878 {
George Liu24fffdc2024-07-17 17:40:53 +0800879 lg2::error("Failed in parsing BMC UUID property, "
880 "interface: {INTERFACE}, property: {PROPERTY}, "
881 "value: {VALUE}, error: {ERROR}",
882 "INTERFACE", uuidInterface, "PROPERTY", uuidProperty,
883 "VALUE", rfc4122Uuid, "ERROR", e);
Vernon Maueryb90a5322019-03-25 13:36:55 -0700884 return ipmi::responseUnspecifiedError();
885 }
886 return ipmi::responseSuccess(uuid);
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600887}
888
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000889/**
890 * @brief set the session state as teardown
891 *
892 * This function is to set the session state to tear down in progress if the
893 * state is active.
894 *
895 * @param[in] busp - Dbus obj
896 * @param[in] service - service name
897 * @param[in] obj - object path
898 *
899 * @return success completion code if it sets the session state to
900 * tearDownInProgress else return the corresponding error completion code.
901 **/
902uint8_t setSessionState(std::shared_ptr<sdbusplus::asio::connection>& busp,
903 const std::string& service, const std::string& obj)
904{
905 try
906 {
907 uint8_t sessionState = std::get<uint8_t>(ipmi::getDbusProperty(
908 *busp, service, obj, session::sessionIntf, "State"));
909
910 if (sessionState == static_cast<uint8_t>(session::State::active))
911 {
912 ipmi::setDbusProperty(
913 *busp, service, obj, session::sessionIntf, "State",
914 static_cast<uint8_t>(session::State::tearDownInProgress));
915 return ipmi::ccSuccess;
916 }
917 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500918 catch (const std::exception& e)
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000919 {
George Liu24fffdc2024-07-17 17:40:53 +0800920 lg2::error("Failed in getting session state property, "
921 "service: {SERVICE}, object path: {OBJECT_PATH}, "
922 "interface: {INTERFACE}, error: {ERROR}",
923 "SERVICE", service, "OBJECT_PATH", obj, "INTERFACE",
924 session::sessionIntf, "ERROR", e);
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000925 return ipmi::ccUnspecifiedError;
926 }
927
928 return ipmi::ccInvalidFieldRequest;
929}
930
931ipmi::RspType<> ipmiAppCloseSession(uint32_t reqSessionId,
932 std::optional<uint8_t> requestSessionHandle)
933{
934 auto busp = getSdBus();
935 uint8_t reqSessionHandle =
936 requestSessionHandle.value_or(session::defaultSessionHandle);
937
938 if (reqSessionId == session::sessionZero &&
939 reqSessionHandle == session::defaultSessionHandle)
940 {
941 return ipmi::response(session::ccInvalidSessionId);
942 }
943
944 if (reqSessionId == session::sessionZero &&
945 reqSessionHandle == session::invalidSessionHandle)
946 {
947 return ipmi::response(session::ccInvalidSessionHandle);
948 }
949
950 if (reqSessionId != session::sessionZero &&
951 reqSessionHandle != session::defaultSessionHandle)
952 {
953 return ipmi::response(ipmi::ccInvalidFieldRequest);
954 }
955
956 try
957 {
958 ipmi::ObjectTree objectTree = ipmi::getAllDbusObjects(
959 *busp, session::sessionManagerRootPath, session::sessionIntf);
960
961 for (auto& objectTreeItr : objectTree)
962 {
963 const std::string obj = objectTreeItr.first;
964
965 if (isSessionObjectMatched(obj, reqSessionId, reqSessionHandle))
966 {
967 auto& serviceMap = objectTreeItr.second;
968
969 // Session id and session handle are unique for each session.
970 // Session id and handler are retrived from the object path and
971 // object path will be unique for each session. Checking if
972 // multiple objects exist with same object path under multiple
973 // services.
974 if (serviceMap.size() != 1)
975 {
976 return ipmi::responseUnspecifiedError();
977 }
978
979 auto itr = serviceMap.begin();
980 const std::string service = itr->first;
981 return ipmi::response(setSessionState(busp, service, obj));
982 }
983 }
984 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500985 catch (const sdbusplus::exception_t& e)
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000986 {
George Liu24fffdc2024-07-17 17:40:53 +0800987 lg2::error("Failed to fetch object from dbus, "
988 "interface: {INTERFACE}, error: {ERROR}",
989 "INTERFACE", session::sessionIntf, "ERROR", e);
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000990 return ipmi::responseUnspecifiedError();
991 }
992
993 return ipmi::responseInvalidFieldRequest();
994}
995
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +0000996uint8_t getTotalSessionCount()
997{
Meera-Kattac1789482021-05-18 09:53:26 +0000998 uint8_t count = 0, ch = 0;
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +0000999
1000 while (ch < ipmi::maxIpmiChannels &&
1001 count < session::maxNetworkInstanceSupported)
1002 {
Jayaprakash Mutyala80207e62020-07-04 16:34:15 +00001003 ipmi::ChannelInfo chInfo{};
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001004 ipmi::getChannelInfo(ch, chInfo);
1005 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) ==
1006 ipmi::EChannelMediumType::lan8032)
1007 {
1008 count++;
1009 }
1010 ch++;
1011 }
1012 return count * session::maxSessionCountPerChannel;
1013}
1014
1015/**
1016 * @brief get session info request data.
1017 *
1018 * This function validates the request data and retrive request session id,
1019 * session handle.
1020 *
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301021 * @param[in] ctx - context of current session.
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001022 * @param[in] sessionIndex - request session index
1023 * @param[in] payload - input payload
1024 * @param[in] reqSessionId - unpacked session Id will be asigned
1025 * @param[in] reqSessionHandle - unpacked session handle will be asigned
1026 *
1027 * @return success completion code if request data is valid
1028 * else return the correcponding error completion code.
1029 **/
Patrick Williams1318a5e2024-08-16 15:19:54 -04001030uint8_t getSessionInfoRequestData(
1031 const ipmi::Context::ptr ctx, const uint8_t sessionIndex,
1032 ipmi::message::Payload& payload, uint32_t& reqSessionId,
1033 uint8_t& reqSessionHandle)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001034{
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301035 if ((sessionIndex > session::maxSessionCountPerChannel) &&
1036 (sessionIndex < session::searchSessionByHandle))
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001037 {
1038 return ipmi::ccInvalidFieldRequest;
1039 }
1040
1041 switch (sessionIndex)
1042 {
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301043 case session::searchCurrentSession:
1044
1045 ipmi::ChannelInfo chInfo;
1046 ipmi::getChannelInfo(ctx->channel, chInfo);
1047
1048 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) !=
1049 ipmi::EChannelMediumType::lan8032)
1050 {
1051 return ipmi::ccInvalidFieldRequest;
1052 }
1053
1054 if (!payload.fullyUnpacked())
1055 {
1056 return ipmi::ccReqDataLenInvalid;
1057 }
1058 // Check if current sessionId is 0, sessionId 0 is reserved.
1059 if (ctx->sessionId == session::sessionZero)
1060 {
1061 return session::ccInvalidSessionId;
1062 }
1063 reqSessionId = ctx->sessionId;
1064 break;
1065
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001066 case session::searchSessionByHandle:
1067
1068 if ((payload.unpack(reqSessionHandle)) ||
1069 (!payload.fullyUnpacked()))
1070 {
1071 return ipmi::ccReqDataLenInvalid;
1072 }
1073
1074 if ((reqSessionHandle == session::sessionZero) ||
1075 ((reqSessionHandle & session::multiIntfaceSessionHandleMask) >
1076 session::maxSessionCountPerChannel))
1077 {
1078 return session::ccInvalidSessionHandle;
1079 }
1080 break;
1081
1082 case session::searchSessionById:
1083
1084 if ((payload.unpack(reqSessionId)) || (!payload.fullyUnpacked()))
1085 {
1086 return ipmi::ccReqDataLenInvalid;
1087 }
1088
1089 if (reqSessionId == session::sessionZero)
1090 {
1091 return session::ccInvalidSessionId;
1092 }
1093 break;
1094
1095 default:
1096 if (!payload.fullyUnpacked())
1097 {
1098 return ipmi::ccReqDataLenInvalid;
1099 }
1100 break;
1101 }
1102 return ipmi::ccSuccess;
1103}
1104
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001105uint8_t getSessionState(ipmi::Context::ptr ctx, const std::string& service,
1106 const std::string& objPath, uint8_t& sessionState)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001107{
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001108 boost::system::error_code ec = ipmi::getDbusProperty(
1109 ctx, service, objPath, session::sessionIntf, "State", sessionState);
1110 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001111 {
George Liu24fffdc2024-07-17 17:40:53 +08001112 lg2::error("Failed to fetch state property, service: {SERVICE}, "
1113 "object path: {OBJECTPATH}, interface: {INTERFACE}, "
1114 "error: {ERROR}",
1115 "SERVICE", service, "OBJECTPATH", objPath, "INTERFACE",
1116 session::sessionIntf, "ERROR", ec.message());
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001117 return ipmi::ccUnspecifiedError;
1118 }
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001119 return ipmi::ccSuccess;
1120}
1121
1122static constexpr uint8_t macAddrLen = 6;
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001123/** Alias SessionDetails - contain the optional information about an
1124 * RMCP+ session.
1125 *
1126 * @param userID - uint6_t session user ID (0-63)
1127 * @param reserved - uint2_t reserved
1128 * @param privilege - uint4_t session privilege (0-5)
1129 * @param reserved - uint4_t reserved
1130 * @param channel - uint4_t session channel number
1131 * @param protocol - uint4_t session protocol
1132 * @param remoteIP - uint32_t remote IP address
1133 * @param macAddr - std::array<uint8_t, 6> mac address
1134 * @param port - uint16_t remote port
1135 */
1136using SessionDetails =
1137 std::tuple<uint2_t, uint6_t, uint4_t, uint4_t, uint4_t, uint4_t, uint32_t,
1138 std::array<uint8_t, macAddrLen>, uint16_t>;
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001139
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001140/** @brief get session details for a given session
1141 *
1142 * @param[in] ctx - ipmi::Context pointer for accessing D-Bus
1143 * @param[in] service - D-Bus service name to fetch details from
1144 * @param[in] objPath - D-Bus object path for session
1145 * @param[out] sessionHandle - return session handle for session
1146 * @param[out] sessionState - return session state for session
1147 * @param[out] details - return a SessionDetails tuple containing other
1148 * session info
1149 * @return - ipmi::Cc success or error code
1150 */
1151ipmi::Cc getSessionDetails(ipmi::Context::ptr ctx, const std::string& service,
1152 const std::string& objPath, uint8_t& sessionHandle,
1153 uint8_t& sessionState, SessionDetails& details)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001154{
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001155 ipmi::PropertyMap sessionProps;
1156 boost::system::error_code ec = ipmi::getAllDbusProperties(
1157 ctx, service, objPath, session::sessionIntf, sessionProps);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001158
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001159 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001160 {
George Liu24fffdc2024-07-17 17:40:53 +08001161 lg2::error("Failed to fetch state property, service: {SERVICE}, "
1162 "object path: {OBJECTPATH}, interface: {INTERFACE}, "
1163 "error: {ERROR}",
1164 "SERVICE", service, "OBJECTPATH", objPath, "INTERFACE",
1165 session::sessionIntf, "ERROR", ec.message());
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001166 return ipmi::ccUnspecifiedError;
1167 }
1168
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001169 sessionState = ipmi::mappedVariant<uint8_t>(
1170 sessionProps, "State", static_cast<uint8_t>(session::State::inactive));
1171 if (sessionState == static_cast<uint8_t>(session::State::active))
1172 {
Patrick Williams1318a5e2024-08-16 15:19:54 -04001173 sessionHandle =
1174 ipmi::mappedVariant<uint8_t>(sessionProps, "SessionHandle", 0);
1175 std::get<0>(details) =
1176 ipmi::mappedVariant<uint8_t>(sessionProps, "UserID", 0xff);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001177 // std::get<1>(details) = 0; // (default constructed to 0)
1178 std::get<2>(details) =
1179 ipmi::mappedVariant<uint8_t>(sessionProps, "CurrentPrivilege", 0);
1180 // std::get<3>(details) = 0; // (default constructed to 0)
Patrick Williams1318a5e2024-08-16 15:19:54 -04001181 std::get<4>(details) =
1182 ipmi::mappedVariant<uint8_t>(sessionProps, "ChannelNum", 0xff);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001183 constexpr uint4_t rmcpPlusProtocol = 1;
1184 std::get<5>(details) = rmcpPlusProtocol;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001185 std::get<6>(details) =
1186 ipmi::mappedVariant<uint32_t>(sessionProps, "RemoteIPAddr", 0);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001187 // std::get<7>(details) = {{0}}; // default constructed to all 0
Patrick Williams1318a5e2024-08-16 15:19:54 -04001188 std::get<8>(details) =
1189 ipmi::mappedVariant<uint16_t>(sessionProps, "RemotePort", 0);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001190 }
1191
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001192 return ipmi::ccSuccess;
1193}
1194
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001195ipmi::RspType<uint8_t, // session handle,
1196 uint8_t, // total session count
1197 uint8_t, // active session count
1198 std::optional<SessionDetails>>
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301199 ipmiAppGetSessionInfo(ipmi::Context::ptr ctx, uint8_t sessionIndex,
1200 ipmi::message::Payload& payload)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001201{
1202 uint32_t reqSessionId = 0;
1203 uint8_t reqSessionHandle = session::defaultSessionHandle;
1204 // initializing state to 0xff as 0 represents state as inactive.
1205 uint8_t state = 0xFF;
1206
1207 uint8_t completionCode = getSessionInfoRequestData(
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301208 ctx, sessionIndex, payload, reqSessionId, reqSessionHandle);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001209
1210 if (completionCode)
1211 {
1212 return ipmi::response(completionCode);
1213 }
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001214 ipmi::ObjectTree objectTree;
1215 boost::system::error_code ec = ipmi::getAllDbusObjects(
1216 ctx, session::sessionManagerRootPath, session::sessionIntf, objectTree);
1217 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001218 {
George Liu24fffdc2024-07-17 17:40:53 +08001219 lg2::error("Failed to fetch object from dbus, "
1220 "interface: {INTERFACE}, error: {ERROR}",
1221 "INTERFACE", session::sessionIntf, "ERROR", ec.message());
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001222 return ipmi::responseUnspecifiedError();
1223 }
1224
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001225 uint8_t totalSessionCount = getTotalSessionCount();
1226 uint8_t activeSessionCount = 0;
1227 uint8_t sessionHandle = session::defaultSessionHandle;
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001228 uint8_t activeSessionHandle = 0;
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001229 std::optional<SessionDetails> maybeDetails;
1230 uint8_t index = 0;
1231 for (auto& objectTreeItr : objectTree)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001232 {
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001233 uint32_t sessionId = 0;
1234 std::string objectPath = objectTreeItr.first;
1235
1236 if (!parseCloseSessionInputPayload(objectPath, sessionId,
1237 sessionHandle))
1238 {
1239 continue;
1240 }
1241 index++;
1242 auto& serviceMap = objectTreeItr.second;
1243 auto itr = serviceMap.begin();
1244
1245 if (serviceMap.size() != 1)
1246 {
1247 return ipmi::responseUnspecifiedError();
1248 }
1249
1250 std::string service = itr->first;
1251 uint8_t sessionState = 0;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001252 completionCode =
1253 getSessionState(ctx, service, objectPath, sessionState);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001254 if (completionCode)
1255 {
1256 return ipmi::response(completionCode);
1257 }
1258
1259 if (sessionState == static_cast<uint8_t>(session::State::active))
1260 {
1261 activeSessionCount++;
1262 }
1263
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001264 if (index == sessionIndex || reqSessionId == sessionId ||
1265 reqSessionHandle == sessionHandle)
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001266 {
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001267 SessionDetails details{};
1268 completionCode = getSessionDetails(ctx, service, objectPath,
1269 sessionHandle, state, details);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001270
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001271 if (completionCode)
1272 {
1273 return ipmi::response(completionCode);
1274 }
1275 activeSessionHandle = sessionHandle;
1276 maybeDetails = std::move(details);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001277 }
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001278 }
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001279
1280 if (state == static_cast<uint8_t>(session::State::active) ||
1281 state == static_cast<uint8_t>(session::State::tearDownInProgress))
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001282 {
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001283 return ipmi::responseSuccess(activeSessionHandle, totalSessionCount,
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001284 activeSessionCount, maybeDetails);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001285 }
1286
1287 return ipmi::responseInvalidFieldRequest();
1288}
1289
Thang Trana809fa52024-08-08 14:46:34 +07001290std::optional<std::string> getSysFWVersion(ipmi::Context::ptr& ctx)
1291{
1292 /*
1293 * The System Firmware version is detected via following steps:
1294 * - Get all of object paths that include
1295 * "xyz.openbmc_project.Software.Version" interface.
1296 * - Get the Purpose property of above object paths.
1297 * - If the Purpose is Host then get the Version property.
1298 */
1299 ipmi::ObjectTree objectTree;
1300 boost::system::error_code ec =
1301 ipmi::getAllDbusObjects(ctx, softwareRoot, versionIntf, objectTree);
1302 if (ec.value())
1303 {
1304 return std::nullopt;
1305 }
1306
1307 for (const auto& [objPath, serviceMap] : objectTree)
1308 {
1309 for (const auto& [service, intfs] : serviceMap)
1310 {
1311 ipmi::PropertyMap props;
1312 ec = ipmi::getAllDbusProperties(ctx, service, objPath, versionIntf,
1313 props);
1314 if (ec.value())
1315 {
1316 continue;
1317 }
1318
1319 std::string purposeProp = std::string(
1320 ipmi::mappedVariant<std::string>(props, "Purpose", ""));
1321
1322 if (!purposeProp.ends_with(versionPurposeHostEnd))
1323 {
1324 continue;
1325 }
1326
1327 std::string sysFWVersion = std::string(
1328 ipmi::mappedVariant<std::string>(props, "Version", ""));
1329
1330 if (sysFWVersion.empty())
1331 {
1332 return std::nullopt;
1333 }
1334
1335 return sysFWVersion;
1336 }
1337 }
1338
1339 return std::nullopt;
1340}
1341
Xo Wangf542e8b2017-08-09 15:34:16 -07001342static std::unique_ptr<SysInfoParamStore> sysInfoParamStore;
1343
Xo Wang87651332017-08-11 10:17:59 -07001344static std::string sysInfoReadSystemName()
1345{
1346 // Use the BMC hostname as the "System Name."
1347 char hostname[HOST_NAME_MAX + 1] = {};
1348 if (gethostname(hostname, HOST_NAME_MAX) != 0)
1349 {
1350 perror("System info parameter: system name");
1351 }
1352 return hostname;
1353}
1354
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001355static constexpr uint8_t paramRevision = 0x11;
1356static constexpr size_t configParameterLength = 16;
Xo Wangf542e8b2017-08-09 15:34:16 -07001357
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001358static constexpr size_t smallChunkSize = 14;
1359static constexpr size_t fullChunkSize = 16;
Jia, chunhui449f2162019-09-11 16:51:51 +08001360static constexpr uint8_t progressMask = 0x3;
krishnar410752832023-11-06 13:58:35 +05301361static constexpr uint8_t maxValidEncodingData = 0x02;
Xo Wangf542e8b2017-08-09 15:34:16 -07001362
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001363static constexpr uint8_t setComplete = 0x0;
1364static constexpr uint8_t setInProgress = 0x1;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001365static uint8_t transferStatus = setComplete;
1366
Jia, chunhui449f2162019-09-11 16:51:51 +08001367static constexpr uint8_t configDataOverhead = 2;
1368
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001369namespace ipmi
1370{
1371constexpr Cc ccParmNotSupported = 0x80;
Jia, chunhui449f2162019-09-11 16:51:51 +08001372constexpr Cc ccSetInProgressActive = 0x81;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001373
1374static inline auto responseParmNotSupported()
1375{
1376 return response(ccParmNotSupported);
Xo Wangf542e8b2017-08-09 15:34:16 -07001377}
Jia, chunhui449f2162019-09-11 16:51:51 +08001378static inline auto responseSetInProgressActive()
1379{
1380 return response(ccSetInProgressActive);
1381}
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001382} // namespace ipmi
Xo Wangf542e8b2017-08-09 15:34:16 -07001383
Jia, chunhui449f2162019-09-11 16:51:51 +08001384ipmi::RspType<uint8_t, // Parameter revision
1385 std::optional<uint8_t>, // data1 / setSelector / ProgressStatus
1386 std::optional<std::vector<uint8_t>>> // data2-17
Thang Trana809fa52024-08-08 14:46:34 +07001387 ipmiAppGetSystemInfo(ipmi::Context::ptr ctx, uint7_t reserved,
1388 bool getRevision, uint8_t paramSelector,
1389 uint8_t setSelector, uint8_t BlockSelector)
Xo Wangf542e8b2017-08-09 15:34:16 -07001390{
Snehalatha Va5ae7722020-05-02 18:18:57 +00001391 if (reserved || (paramSelector >= invalidParamSelectorStart &&
1392 paramSelector <= invalidParamSelectorEnd))
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001393 {
1394 return ipmi::responseInvalidFieldRequest();
1395 }
PavanKumarIntel3771f5f2023-11-02 06:26:42 +00001396 if (paramSelector >= oemCmdStart)
Snehalatha Va5ae7722020-05-02 18:18:57 +00001397 {
1398 return ipmi::responseParmNotSupported();
1399 }
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001400 if (getRevision)
Xo Wangf542e8b2017-08-09 15:34:16 -07001401 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001402 return ipmi::responseSuccess(paramRevision, std::nullopt, std::nullopt);
Xo Wangf542e8b2017-08-09 15:34:16 -07001403 }
1404
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001405 if (paramSelector == 0)
Xo Wangf542e8b2017-08-09 15:34:16 -07001406 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001407 return ipmi::responseSuccess(paramRevision, transferStatus,
1408 std::nullopt);
Xo Wangf542e8b2017-08-09 15:34:16 -07001409 }
1410
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001411 if (BlockSelector != 0) // 00h if parameter does not require a block number
Xo Wangf542e8b2017-08-09 15:34:16 -07001412 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001413 return ipmi::responseParmNotSupported();
Xo Wangf542e8b2017-08-09 15:34:16 -07001414 }
1415
1416 if (sysInfoParamStore == nullptr)
1417 {
1418 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
Xo Wang87651332017-08-11 10:17:59 -07001419 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1420 sysInfoReadSystemName);
Xo Wangf542e8b2017-08-09 15:34:16 -07001421 }
1422
Thang Trana809fa52024-08-08 14:46:34 +07001423 if (paramSelector == IPMI_SYSINFO_SYSTEM_FW_VERSION)
1424 {
Prithvi Paid20670b2025-08-01 20:11:28 +05301425 // If the system firmware version is not cached, get it from the D-Bus.
1426 const auto& [found, paramString] =
1427 sysInfoParamStore->lookup(IPMI_SYSINFO_SYSTEM_FW_VERSION);
Thang Trana809fa52024-08-08 14:46:34 +07001428
Prithvi Paid20670b2025-08-01 20:11:28 +05301429 if (!found || paramString.empty())
Thang Trana809fa52024-08-08 14:46:34 +07001430 {
Prithvi Paid20670b2025-08-01 20:11:28 +05301431 auto fwVersion = getSysFWVersion(ctx);
1432 if (fwVersion == std::nullopt)
1433 {
1434 return ipmi::responseUnspecifiedError();
1435 }
1436 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_FW_VERSION,
1437 *fwVersion);
Thang Trana809fa52024-08-08 14:46:34 +07001438 }
Thang Trana809fa52024-08-08 14:46:34 +07001439 }
1440
Xo Wangf542e8b2017-08-09 15:34:16 -07001441 // Parameters other than Set In Progress are assumed to be strings.
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001442 std::tuple<bool, std::string> ret =
1443 sysInfoParamStore->lookup(paramSelector);
1444 bool found = std::get<0>(ret);
Xo Wangf542e8b2017-08-09 15:34:16 -07001445 if (!found)
1446 {
Snehalatha Va5ae7722020-05-02 18:18:57 +00001447 return ipmi::responseSensorInvalid();
Xo Wangf542e8b2017-08-09 15:34:16 -07001448 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001449 std::string& paramString = std::get<1>(ret);
Jia, chunhui449f2162019-09-11 16:51:51 +08001450 std::vector<uint8_t> configData;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001451 size_t count = 0;
1452 if (setSelector == 0)
Jia, chunhui449f2162019-09-11 16:51:51 +08001453 { // First chunk has only 14 bytes.
1454 configData.emplace_back(0); // encoding
1455 configData.emplace_back(paramString.length()); // string length
1456 count = std::min(paramString.length(), smallChunkSize);
1457 configData.resize(count + configDataOverhead);
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001458 std::copy_n(paramString.begin(), count,
Snehalatha Va5ae7722020-05-02 18:18:57 +00001459 configData.begin() + configDataOverhead); // 14 bytes chunk
1460
1461 // Append zero's to remaining bytes
1462 if (configData.size() < configParameterLength)
1463 {
1464 std::fill_n(std::back_inserter(configData),
1465 configParameterLength - configData.size(), 0x00);
1466 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001467 }
1468 else
Xo Wangf542e8b2017-08-09 15:34:16 -07001469 {
Jia, chunhui449f2162019-09-11 16:51:51 +08001470 size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001471 if (offset >= paramString.length())
1472 {
1473 return ipmi::responseParmOutOfRange();
1474 }
Jia, chunhui449f2162019-09-11 16:51:51 +08001475 count = std::min(paramString.length() - offset, fullChunkSize);
1476 configData.resize(count);
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001477 std::copy_n(paramString.begin() + offset, count,
1478 configData.begin()); // 16 bytes chunk
Xo Wangf542e8b2017-08-09 15:34:16 -07001479 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001480 return ipmi::responseSuccess(paramRevision, setSelector, configData);
Xo Wangf542e8b2017-08-09 15:34:16 -07001481}
1482
Jia, chunhui449f2162019-09-11 16:51:51 +08001483ipmi::RspType<> ipmiAppSetSystemInfo(uint8_t paramSelector, uint8_t data1,
1484 std::vector<uint8_t> configData)
1485{
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001486 if (paramSelector >= invalidParamSelectorStart &&
1487 paramSelector <= invalidParamSelectorEnd)
1488 {
1489 return ipmi::responseInvalidFieldRequest();
1490 }
PavanKumarIntel3771f5f2023-11-02 06:26:42 +00001491 if (paramSelector >= oemCmdStart)
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001492 {
1493 return ipmi::responseParmNotSupported();
1494 }
1495
Jia, chunhui449f2162019-09-11 16:51:51 +08001496 if (paramSelector == 0)
1497 {
1498 // attempt to set the 'set in progress' value (in parameter #0)
1499 // when not in the set complete state.
1500 if ((transferStatus != setComplete) && (data1 == setInProgress))
1501 {
1502 return ipmi::responseSetInProgressActive();
1503 }
1504 // only following 2 states are supported
1505 if (data1 > setInProgress)
1506 {
George Liu24fffdc2024-07-17 17:40:53 +08001507 lg2::error("illegal SetInProgress status");
Jia, chunhui449f2162019-09-11 16:51:51 +08001508 return ipmi::responseInvalidFieldRequest();
1509 }
1510
1511 transferStatus = data1 & progressMask;
1512 return ipmi::responseSuccess();
1513 }
1514
1515 if (configData.size() > configParameterLength)
1516 {
1517 return ipmi::responseInvalidFieldRequest();
1518 }
1519
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001520 // Append zero's to remaining bytes
1521 if (configData.size() < configParameterLength)
1522 {
1523 fill_n(back_inserter(configData),
1524 (configParameterLength - configData.size()), 0x00);
1525 }
1526
Jia, chunhui449f2162019-09-11 16:51:51 +08001527 if (!sysInfoParamStore)
1528 {
1529 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
1530 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1531 sysInfoReadSystemName);
1532 }
1533
1534 // lookup
1535 std::tuple<bool, std::string> ret =
1536 sysInfoParamStore->lookup(paramSelector);
1537 bool found = std::get<0>(ret);
1538 std::string& paramString = std::get<1>(ret);
1539 if (!found)
1540 {
1541 // parameter does not exist. Init new
1542 paramString = "";
1543 }
1544
1545 uint8_t setSelector = data1;
1546 size_t count = 0;
krishnar410752832023-11-06 13:58:35 +05301547 if (setSelector == 0) // First chunk has only 14 bytes.
Jia, chunhui449f2162019-09-11 16:51:51 +08001548 {
krishnar410752832023-11-06 13:58:35 +05301549 uint8_t encoding = configData.at(0);
1550 if (encoding > maxValidEncodingData)
1551 {
1552 return ipmi::responseInvalidFieldRequest();
1553 }
1554
Jia, chunhui449f2162019-09-11 16:51:51 +08001555 size_t stringLen = configData.at(1); // string length
Jia, chunhui449f2162019-09-11 16:51:51 +08001556 count = std::min(stringLen, smallChunkSize);
1557 count = std::min(count, configData.size());
1558 paramString.resize(stringLen); // reserve space
1559 std::copy_n(configData.begin() + configDataOverhead, count,
1560 paramString.begin());
1561 }
1562 else
1563 {
1564 size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
1565 if (offset >= paramString.length())
1566 {
1567 return ipmi::responseParmOutOfRange();
1568 }
1569 count = std::min(paramString.length() - offset, configData.size());
1570 std::copy_n(configData.begin(), count, paramString.begin() + offset);
1571 }
1572 sysInfoParamStore->update(paramSelector, paramString);
1573 return ipmi::responseSuccess();
1574}
1575
Yong Libd0503a2019-08-22 17:17:17 +08001576#ifdef ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301577inline std::vector<uint8_t> convertStringToData(const std::string& command)
1578{
1579 std::istringstream iss(command);
1580 std::string token;
1581 std::vector<uint8_t> dataValue;
1582 while (std::getline(iss, token, ' '))
1583 {
1584 dataValue.emplace_back(
1585 static_cast<uint8_t>(std::stoul(token, nullptr, base_16)));
1586 }
1587 return dataValue;
1588}
1589
Matt Simmering68d9d402023-11-09 14:22:11 -08001590static bool populateI2CControllerWRAllowlist()
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301591{
1592 nlohmann::json data = nullptr;
Matt Simmering68d9d402023-11-09 14:22:11 -08001593 std::ifstream jsonFile(i2cControllerWRAllowlistFile);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301594
1595 if (!jsonFile.good())
1596 {
George Liu24fffdc2024-07-17 17:40:53 +08001597 lg2::warning("i2c allow list file not found! file name: {FILE_NAME}",
1598 "FILE_NAME", i2cControllerWRAllowlistFile);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301599 return false;
1600 }
1601
1602 try
1603 {
1604 data = nlohmann::json::parse(jsonFile, nullptr, false);
1605 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001606 catch (const nlohmann::json::parse_error& e)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301607 {
George Liu24fffdc2024-07-17 17:40:53 +08001608 lg2::error("Corrupted i2c allow list config file, "
1609 "file name: {FILE_NAME}, error: {ERROR}",
1610 "FILE_NAME", i2cControllerWRAllowlistFile, "ERROR", e);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301611 return false;
1612 }
1613
1614 try
1615 {
1616 // Example JSON Structure format
1617 // "filters": [
1618 // {
1619 // "Description": "Allow full read - ignore first byte write value
1620 // for 0x40 to 0x4F",
1621 // "busId": "0x01",
1622 // "slaveAddr": "0x40",
1623 // "slaveAddrMask": "0x0F",
1624 // "command": "0x00",
1625 // "commandMask": "0xFF"
1626 // },
1627 // {
1628 // "Description": "Allow full read - first byte match 0x05 and
1629 // ignore second byte",
1630 // "busId": "0x01",
1631 // "slaveAddr": "0x57",
1632 // "slaveAddrMask": "0x00",
1633 // "command": "0x05 0x00",
1634 // "commandMask": "0x00 0xFF"
1635 // },]
1636
1637 nlohmann::json filters = data[filtersStr].get<nlohmann::json>();
Matt Simmering68d9d402023-11-09 14:22:11 -08001638 std::vector<i2cControllerWRAllowlist>& allowlist = getWRAllowlist();
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301639 for (const auto& it : filters.items())
1640 {
1641 nlohmann::json filter = it.value();
1642 if (filter.is_null())
1643 {
George Liu24fffdc2024-07-17 17:40:53 +08001644 lg2::error(
1645 "Corrupted I2C controller write read allowlist config file, "
1646 "file name: {FILE_NAME}",
1647 "FILE_NAME", i2cControllerWRAllowlistFile);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301648 return false;
1649 }
1650 const std::vector<uint8_t>& writeData =
1651 convertStringToData(filter[cmdStr].get<std::string>());
1652 const std::vector<uint8_t>& writeDataMask =
1653 convertStringToData(filter[cmdMaskStr].get<std::string>());
1654 if (writeDataMask.size() != writeData.size())
1655 {
George Liu24fffdc2024-07-17 17:40:53 +08001656 lg2::error("I2C controller write read allowlist filter "
1657 "mismatch for command & mask size");
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301658 return false;
1659 }
Matt Simmering68d9d402023-11-09 14:22:11 -08001660 allowlist.push_back(
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301661 {static_cast<uint8_t>(std::stoul(
1662 filter[busIdStr].get<std::string>(), nullptr, base_16)),
1663 static_cast<uint8_t>(
Matt Simmering68d9d402023-11-09 14:22:11 -08001664 std::stoul(filter[targetAddrStr].get<std::string>(),
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301665 nullptr, base_16)),
1666 static_cast<uint8_t>(
Matt Simmering68d9d402023-11-09 14:22:11 -08001667 std::stoul(filter[targetAddrMaskStr].get<std::string>(),
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301668 nullptr, base_16)),
1669 writeData, writeDataMask});
1670 }
Matt Simmering68d9d402023-11-09 14:22:11 -08001671 if (allowlist.size() != filters.size())
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301672 {
George Liu24fffdc2024-07-17 17:40:53 +08001673 lg2::error(
Matt Simmering68d9d402023-11-09 14:22:11 -08001674 "I2C controller write read allowlist filter size mismatch");
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301675 return false;
1676 }
1677 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001678 catch (const std::exception& e)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301679 {
George Liu24fffdc2024-07-17 17:40:53 +08001680 lg2::error("I2C controller write read allowlist "
1681 "unexpected exception: {ERROR}",
1682 "ERROR", e);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301683 return false;
1684 }
1685 return true;
1686}
1687
Matt Simmering68d9d402023-11-09 14:22:11 -08001688static inline bool isWriteDataAllowlisted(const std::vector<uint8_t>& data,
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301689 const std::vector<uint8_t>& dataMask,
1690 const std::vector<uint8_t>& writeData)
1691{
1692 std::vector<uint8_t> processedDataBuf(data.size());
1693 std::vector<uint8_t> processedReqBuf(dataMask.size());
1694 std::transform(writeData.begin(), writeData.end(), dataMask.begin(),
1695 processedReqBuf.begin(), std::bit_or<uint8_t>());
1696 std::transform(data.begin(), data.end(), dataMask.begin(),
1697 processedDataBuf.begin(), std::bit_or<uint8_t>());
1698
1699 return (processedDataBuf == processedReqBuf);
1700}
1701
Matt Simmering68d9d402023-11-09 14:22:11 -08001702static bool isCmdAllowlisted(uint8_t busId, uint8_t targetAddr,
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301703 std::vector<uint8_t>& writeData)
1704{
Matt Simmering68d9d402023-11-09 14:22:11 -08001705 std::vector<i2cControllerWRAllowlist>& allowList = getWRAllowlist();
1706 for (const auto& wlEntry : allowList)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301707 {
1708 if ((busId == wlEntry.busId) &&
Matt Simmering68d9d402023-11-09 14:22:11 -08001709 ((targetAddr | wlEntry.targetAddrMask) ==
1710 (wlEntry.targetAddr | wlEntry.targetAddrMask)))
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301711 {
1712 const std::vector<uint8_t>& dataMask = wlEntry.dataMask;
1713 // Skip as no-match, if requested write data is more than the
1714 // write data mask size
1715 if (writeData.size() > dataMask.size())
1716 {
1717 continue;
1718 }
Matt Simmering68d9d402023-11-09 14:22:11 -08001719 if (isWriteDataAllowlisted(wlEntry.data, dataMask, writeData))
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301720 {
1721 return true;
1722 }
1723 }
1724 }
1725 return false;
1726}
Yong Libd0503a2019-08-22 17:17:17 +08001727#else
Matt Simmering68d9d402023-11-09 14:22:11 -08001728static bool populateI2CControllerWRAllowlist()
Yong Libd0503a2019-08-22 17:17:17 +08001729{
George Liu24fffdc2024-07-17 17:40:53 +08001730 lg2::info("I2C_WHITELIST_CHECK is disabled, do not populate allowlist");
Yong Libd0503a2019-08-22 17:17:17 +08001731 return true;
1732}
1733#endif // ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301734
Matt Simmering68d9d402023-11-09 14:22:11 -08001735/** @brief implements controller write read IPMI command which can be used for
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301736 * low-level I2C/SMBus write, read or write-read access
1737 * @param isPrivateBus -to indicate private bus usage
1738 * @param busId - bus id
1739 * @param channelNum - channel number
1740 * @param reserved - skip 1 bit
Matt Simmering68d9d402023-11-09 14:22:11 -08001741 * @param targetAddr - target address
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301742 * @param read count - number of bytes to be read
1743 * @param writeData - data to be written
1744 *
1745 * @returns IPMI completion code plus response data
1746 * - readData - i2c response data
1747 */
Patrick Williams1318a5e2024-08-16 15:19:54 -04001748ipmi::RspType<std::vector<uint8_t>> ipmiControllerWriteRead(
1749 [[maybe_unused]] bool isPrivateBus, uint3_t busId,
1750 [[maybe_unused]] uint4_t channelNum, bool reserved, uint7_t targetAddr,
1751 uint8_t readCount, std::vector<uint8_t> writeData)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301752{
Jayaprakash Mutyalac2af98b2021-09-14 09:19:11 +00001753 if (reserved)
1754 {
1755 return ipmi::responseInvalidFieldRequest();
1756 }
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301757 const size_t writeCount = writeData.size();
1758 if (!readCount && !writeCount)
1759 {
George Liu24fffdc2024-07-17 17:40:53 +08001760 lg2::error("Controller write read command: Read & write count are 0");
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301761 return ipmi::responseInvalidFieldRequest();
1762 }
Yong Libd0503a2019-08-22 17:17:17 +08001763#ifdef ENABLE_I2C_WHITELIST_CHECK
Matt Simmering68d9d402023-11-09 14:22:11 -08001764 if (!isCmdAllowlisted(static_cast<uint8_t>(busId),
1765 static_cast<uint8_t>(targetAddr), writeData))
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301766 {
George Liu24fffdc2024-07-17 17:40:53 +08001767 lg2::error("Controller write read request blocked!, "
1768 "bus: {BUS}, addr: {ADDR}",
1769 "BUS", static_cast<uint8_t>(busId), "ADDR", lg2::hex,
1770 static_cast<uint8_t>(targetAddr));
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301771 }
Yong Libd0503a2019-08-22 17:17:17 +08001772#endif // ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301773 std::vector<uint8_t> readBuf(readCount);
Patrick Williams1318a5e2024-08-16 15:19:54 -04001774 std::string i2cBus =
1775 "/dev/i2c-" + std::to_string(static_cast<uint8_t>(busId));
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301776
Matt Simmering68d9d402023-11-09 14:22:11 -08001777 ipmi::Cc ret = ipmi::i2cWriteRead(i2cBus, static_cast<uint8_t>(targetAddr),
Yong Li7dc4ac02019-08-23 17:44:32 +08001778 writeData, readBuf);
1779 if (ret != ipmi::ccSuccess)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301780 {
Yong Li7dc4ac02019-08-23 17:44:32 +08001781 return ipmi::response(ret);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301782 }
1783 return ipmi::responseSuccess(readBuf);
1784}
1785
George Liu5087b072025-03-11 19:28:17 +08001786void registerNetFnAppFunctions()
vishwabmcba0bd5f2015-09-30 16:50:23 +05301787{
Vernon Mauery86a50822019-03-25 13:11:36 -07001788 // <Get Device ID>
1789 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1790 ipmi::app::cmdGetDeviceId, ipmi::Privilege::User,
1791 ipmiAppGetDeviceId);
1792
Tom05732372016-09-06 17:21:23 +05301793 // <Get BT Interface Capabilities>
Vernon Mauerycc99ba42019-03-25 13:40:11 -07001794 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1795 ipmi::app::cmdGetBtIfaceCapabilities,
1796 ipmi::Privilege::User, ipmiAppGetBtCapabilities);
Chris Austen6caf28b2015-10-13 12:40:40 -05001797
Tom05732372016-09-06 17:21:23 +05301798 // <Reset Watchdog Timer>
Vernon Mauery11df4f62019-03-25 14:17:54 -07001799 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1800 ipmi::app::cmdResetWatchdogTimer,
1801 ipmi::Privilege::Operator, ipmiAppResetWatchdogTimer);
Chris Austen6caf28b2015-10-13 12:40:40 -05001802
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001803 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301804 ipmi::app::cmdGetSessionInfo, ipmi::Privilege::User,
1805 ipmiAppGetSessionInfo);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001806
Tom05732372016-09-06 17:21:23 +05301807 // <Set Watchdog Timer>
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +00001808 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1809 ipmi::app::cmdSetWatchdogTimer,
1810 ipmi::Privilege::Operator, ipmiSetWatchdogTimer);
Chris Austen6caf28b2015-10-13 12:40:40 -05001811
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +00001812 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1813 ipmi::app::cmdCloseSession, ipmi::Privilege::Callback,
1814 ipmiAppCloseSession);
1815
William A. Kennington III73f44512018-02-09 15:28:46 -08001816 // <Get Watchdog Timer>
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +00001817 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301818 ipmi::app::cmdGetWatchdogTimer, ipmi::Privilege::User,
1819 ipmiGetWatchdogTimer);
William A. Kennington III73f44512018-02-09 15:28:46 -08001820
Tom05732372016-09-06 17:21:23 +05301821 // <Get Self Test Results>
Vernon Maueryb84a5282019-03-25 13:39:03 -07001822 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1823 ipmi::app::cmdGetSelfTestResults,
1824 ipmi::Privilege::User, ipmiAppGetSelfTestResults);
Nan Li41fa24a2016-11-10 20:12:37 +08001825
Tom05732372016-09-06 17:21:23 +05301826 // <Get Device GUID>
Vernon Mauery15541322019-03-25 13:33:03 -07001827 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1828 ipmi::app::cmdGetDeviceGuid, ipmi::Privilege::User,
1829 ipmiAppGetDeviceGuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -05001830
Tom05732372016-09-06 17:21:23 +05301831 // <Set ACPI Power State>
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +00001832 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1833 ipmi::app::cmdSetAcpiPowerState,
1834 ipmi::Privilege::Admin, ipmiSetAcpiPowerState);
Yong Li18d77262018-10-09 01:59:45 +08001835 // <Get ACPI Power State>
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +00001836 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1837 ipmi::app::cmdGetAcpiPowerState,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301838 ipmi::Privilege::User, ipmiGetAcpiPowerState);
Yong Li18d77262018-10-09 01:59:45 +08001839
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301840 // Note: For security reason, this command will be registered only when
Matt Simmering68d9d402023-11-09 14:22:11 -08001841 // there are proper I2C Controller write read allowlist
1842 if (populateI2CControllerWRAllowlist())
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301843 {
Matt Simmering68d9d402023-11-09 14:22:11 -08001844 // Note: For security reasons, registering controller write read as
1845 // admin privilege command, even though IPMI 2.0 specification allows it
1846 // as operator privilege.
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301847 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1848 ipmi::app::cmdMasterWriteRead,
Matt Simmering68d9d402023-11-09 14:22:11 -08001849 ipmi::Privilege::Admin, ipmiControllerWriteRead);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301850 }
1851
Marri Devender Rao5e007a52018-01-08 06:18:36 -06001852 // <Get System GUID Command>
Vernon Maueryb90a5322019-03-25 13:36:55 -07001853 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1854 ipmi::app::cmdGetSystemGuid, ipmi::Privilege::User,
1855 ipmiAppGetSystemGuid);
Tom Joseph7cbe2282018-03-21 21:17:33 +05301856
1857 // <Get Channel Cipher Suites Command>
Ayushi Smriti5c3b72c2019-08-30 13:47:31 +00001858 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1859 ipmi::app::cmdGetChannelCipherSuites,
Vernon Mauery79b4eea2019-11-07 09:51:39 -08001860 ipmi::Privilege::None, getChannelCipherSuites);
Vernon Maueryd2a57de2019-03-25 12:45:28 -07001861
Xo Wangf542e8b2017-08-09 15:34:16 -07001862 // <Get System Info Command>
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001863 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1864 ipmi::app::cmdGetSystemInfoParameters,
1865 ipmi::Privilege::User, ipmiAppGetSystemInfo);
Jia, chunhui449f2162019-09-11 16:51:51 +08001866 // <Set System Info Command>
1867 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1868 ipmi::app::cmdSetSystemInfoParameters,
1869 ipmi::Privilege::Admin, ipmiAppSetSystemInfo);
vishwabmcba0bd5f2015-09-30 16:50:23 +05301870 return;
1871}