blob: 65390c1b47658d6147e3090625ff697be82533bd [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>
Patrick Venture0b02be92018-08-31 11:55:55 -07008#include <mapper.h>
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05309#include <sys/ioctl.h>
10#include <sys/stat.h>
11#include <sys/types.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070012#include <systemd/sd-bus.h>
Xo Wang87651332017-08-11 10:17:59 -070013#include <unistd.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070014
Vernon Mauery0120b682019-03-25 13:08:54 -070015#include <app/channel.hpp>
16#include <app/watchdog.hpp>
17#include <apphandler.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050018#include <ipmid/api.hpp>
19#include <ipmid/sessiondef.hpp>
20#include <ipmid/sessionhelper.hpp>
21#include <ipmid/types.hpp>
22#include <ipmid/utils.hpp>
23#include <nlohmann/json.hpp>
24#include <phosphor-logging/elog-errors.hpp>
25#include <phosphor-logging/log.hpp>
26#include <sdbusplus/message/types.hpp>
27#include <sys_info_param.hpp>
28#include <xyz/openbmc_project/Common/error.hpp>
29#include <xyz/openbmc_project/Control/Power/ACPIPowerState/server.hpp>
30#include <xyz/openbmc_project/Software/Activation/server.hpp>
31#include <xyz/openbmc_project/Software/Version/server.hpp>
32#include <xyz/openbmc_project/State/BMC/server.hpp>
33
34#include <algorithm>
Patrick Venture3a5071a2018-09-12 13:27:42 -070035#include <array>
Willy Tu886d6842022-06-03 02:50:47 -070036#include <charconv>
Patrick Venture3a5071a2018-09-12 13:27:42 -070037#include <cstddef>
Vernon Mauery0120b682019-03-25 13:08:54 -070038#include <cstdint>
Vernon Mauerybdda8002019-02-26 10:18:51 -080039#include <filesystem>
Patrick Venture3a5071a2018-09-12 13:27:42 -070040#include <fstream>
41#include <memory>
Potin Lai5d214202023-01-16 18:11:49 +080042#include <regex>
Patrick Venture3a5071a2018-09-12 13:27:42 -070043#include <string>
Willy Tu886d6842022-06-03 02:50:47 -070044#include <string_view>
Patrick Venture3a5071a2018-09-12 13:27:42 -070045#include <tuple>
46#include <vector>
Ratan Guptab8e99552017-07-27 07:07:48 +053047
Patrick Venture0b02be92018-08-31 11:55:55 -070048extern sd_bus* bus;
vishwabmcba0bd5f2015-09-30 16:50:23 +053049
Alexander Amelkinba19c182018-09-04 15:49:36 +030050constexpr auto bmc_state_interface = "xyz.openbmc_project.State.BMC";
51constexpr auto bmc_state_property = "CurrentBMCState";
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
Chris Austen6caf28b2015-10-13 12:40:40 -050060void register_netfn_app_functions() __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 Williamsfbc6c9d2023-05-10 07:50:16 -0500118 objectTree = ipmi::getAllDbusObjects(*ctx->bus, softwareRoot,
119 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 {
123 log<level::ERR>("Failed to fetch redundancy object from dbus",
124 entry("INTERFACE=%s", redundancyIntf),
125 entry("ERRMSG=%s", e.what()));
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600126 elog<InternalFailure>();
127 }
128
129 auto objectFound = false;
130 for (auto& softObject : objectTree)
131 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500132 auto service = ipmi::getService(*ctx->bus, redundancyIntf,
133 softObject.first);
134 auto objValueTree = ipmi::getManagedObjects(*ctx->bus, service,
135 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 {
169 log<level::ERR>(e.what());
170 }
171 }
172 }
173
174 if (!objectFound)
175 {
176 log<level::ERR>("Could not found an BMC software Object");
177 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 Williamsfbc6c9d2023-05-10 07:50:16 -0500188 ipmi::DbusObjectInfo bmcObject = ipmi::getDbusObject(bus,
189 bmc_state_interface);
190 auto variant = ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first,
191 bmc_state_interface,
192 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
217const static constexpr char* acpiObjPath =
218 "/xyz/openbmc_project/control/host0/acpi_power_state";
219const static constexpr char* acpiInterface =
220 "xyz.openbmc_project.Control.Power.ACPIPowerState";
221const static constexpr char* sysACPIProp = "SysACPIStatus";
222const static constexpr char* devACPIProp = "DevACPIStatus";
223
224enum class PowerStateType : uint8_t
225{
226 sysPowerState = 0x00,
227 devPowerState = 0x01,
228};
229
230// Defined in 20.6 of ipmi doc
231enum class PowerState : uint8_t
232{
233 s0G0D0 = 0x00,
234 s1D1 = 0x01,
235 s2D2 = 0x02,
236 s3D3 = 0x03,
237 s4 = 0x04,
238 s5G2 = 0x05,
239 s4S5 = 0x06,
240 g3 = 0x07,
241 sleep = 0x08,
242 g1Sleep = 0x09,
243 override = 0x0a,
244 legacyOn = 0x20,
245 legacyOff = 0x21,
246 unknown = 0x2a,
247 noChange = 0x7f,
248};
249
250static constexpr uint8_t stateChanged = 0x80;
251
Yong Li18d77262018-10-09 01:59:45 +0800252std::map<ACPIPowerState::ACPI, PowerState> dbusToIPMI = {
253 {ACPIPowerState::ACPI::S0_G0_D0, PowerState::s0G0D0},
254 {ACPIPowerState::ACPI::S1_D1, PowerState::s1D1},
255 {ACPIPowerState::ACPI::S2_D2, PowerState::s2D2},
256 {ACPIPowerState::ACPI::S3_D3, PowerState::s3D3},
257 {ACPIPowerState::ACPI::S4, PowerState::s4},
258 {ACPIPowerState::ACPI::S5_G2, PowerState::s5G2},
259 {ACPIPowerState::ACPI::S4_S5, PowerState::s4S5},
260 {ACPIPowerState::ACPI::G3, PowerState::g3},
261 {ACPIPowerState::ACPI::SLEEP, PowerState::sleep},
262 {ACPIPowerState::ACPI::G1_SLEEP, PowerState::g1Sleep},
263 {ACPIPowerState::ACPI::OVERRIDE, PowerState::override},
264 {ACPIPowerState::ACPI::LEGACY_ON, PowerState::legacyOn},
265 {ACPIPowerState::ACPI::LEGACY_OFF, PowerState::legacyOff},
266 {ACPIPowerState::ACPI::Unknown, PowerState::unknown}};
267
268bool isValidACPIState(acpi_state::PowerStateType type, uint8_t state)
269{
270 if (type == acpi_state::PowerStateType::sysPowerState)
271 {
272 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::override)) ||
273 (state == static_cast<uint8_t>(acpi_state::PowerState::legacyOn)) ||
274 (state ==
275 static_cast<uint8_t>(acpi_state::PowerState::legacyOff)) ||
276 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
277 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
278 {
279 return true;
280 }
281 else
282 {
283 return false;
284 }
285 }
286 else if (type == acpi_state::PowerStateType::devPowerState)
287 {
288 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::s3D3)) ||
289 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
290 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
291 {
292 return true;
293 }
294 else
295 {
296 return false;
297 }
298 }
299 else
300 {
301 return false;
302 }
303 return false;
304}
305} // namespace acpi_state
306
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000307/** @brief implements Set ACPI Power State command
308 * @param sysAcpiState - ACPI system power state to set
309 * @param devAcpiState - ACPI device power state to set
310 *
311 * @return IPMI completion code on success
312 **/
313ipmi::RspType<> ipmiSetAcpiPowerState(uint8_t sysAcpiState,
314 uint8_t devAcpiState)
Chris Austen6caf28b2015-10-13 12:40:40 -0500315{
Yong Li18d77262018-10-09 01:59:45 +0800316 auto s = static_cast<uint8_t>(acpi_state::PowerState::unknown);
Yong Li18d77262018-10-09 01:59:45 +0800317
Patrick Williams5d82f472022-07-22 19:26:53 -0500318 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Yong Li18d77262018-10-09 01:59:45 +0800319
320 auto value = acpi_state::ACPIPowerState::ACPI::Unknown;
321
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000322 if (sysAcpiState & acpi_state::stateChanged)
Yong Li18d77262018-10-09 01:59:45 +0800323 {
324 // set system power state
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000325 s = sysAcpiState & ~acpi_state::stateChanged;
Yong Li18d77262018-10-09 01:59:45 +0800326
327 if (!acpi_state::isValidACPIState(
328 acpi_state::PowerStateType::sysPowerState, s))
329 {
330 log<level::ERR>("set_acpi_power sys invalid input",
331 entry("S=%x", s));
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000332 return ipmi::responseParmOutOfRange();
Yong Li18d77262018-10-09 01:59:45 +0800333 }
334
335 // valid input
336 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
337 {
338 log<level::DEBUG>("No change for system power state");
339 }
340 else
341 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500342 auto found = std::find_if(acpi_state::dbusToIPMI.begin(),
343 acpi_state::dbusToIPMI.end(),
344 [&s](const auto& iter) {
345 return (static_cast<uint8_t>(iter.second) == s);
346 });
Yong Li18d77262018-10-09 01:59:45 +0800347
348 value = found->first;
349
350 try
351 {
352 auto acpiObject =
353 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
354 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
355 acpi_state::acpiInterface,
356 acpi_state::sysACPIProp,
357 convertForMessage(value));
358 }
359 catch (const InternalFailure& e)
360 {
361 log<level::ERR>("Failed in set ACPI system property",
362 entry("EXCEPTION=%s", e.what()));
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000363 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800364 }
365 }
366 }
367 else
368 {
369 log<level::DEBUG>("Do not change system power state");
370 }
371
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000372 if (devAcpiState & acpi_state::stateChanged)
Yong Li18d77262018-10-09 01:59:45 +0800373 {
374 // set device power state
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000375 s = devAcpiState & ~acpi_state::stateChanged;
Yong Li18d77262018-10-09 01:59:45 +0800376 if (!acpi_state::isValidACPIState(
377 acpi_state::PowerStateType::devPowerState, s))
378 {
379 log<level::ERR>("set_acpi_power dev invalid input",
380 entry("S=%x", s));
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000381 return ipmi::responseParmOutOfRange();
Yong Li18d77262018-10-09 01:59:45 +0800382 }
383
384 // valid input
385 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
386 {
387 log<level::DEBUG>("No change for device power state");
388 }
389 else
390 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500391 auto found = std::find_if(acpi_state::dbusToIPMI.begin(),
392 acpi_state::dbusToIPMI.end(),
393 [&s](const auto& iter) {
394 return (static_cast<uint8_t>(iter.second) == s);
395 });
Yong Li18d77262018-10-09 01:59:45 +0800396
397 value = found->first;
398
399 try
400 {
401 auto acpiObject =
402 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
403 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
404 acpi_state::acpiInterface,
405 acpi_state::devACPIProp,
406 convertForMessage(value));
407 }
408 catch (const InternalFailure& e)
409 {
410 log<level::ERR>("Failed in set ACPI device property",
411 entry("EXCEPTION=%s", e.what()));
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000412 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800413 }
414 }
415 }
416 else
417 {
418 log<level::DEBUG>("Do not change device power state");
419 }
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000420 return ipmi::responseSuccess();
Yong Li18d77262018-10-09 01:59:45 +0800421}
422
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000423/**
424 * @brief implements the get ACPI power state command
425 *
426 * @return IPMI completion code plus response data on success.
427 * - ACPI system power state
428 * - ACPI device power state
429 **/
430ipmi::RspType<uint8_t, // acpiSystemPowerState
431 uint8_t // acpiDevicePowerState
432 >
433 ipmiGetAcpiPowerState()
Yong Li18d77262018-10-09 01:59:45 +0800434{
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000435 uint8_t sysAcpiState;
436 uint8_t devAcpiState;
Yong Li18d77262018-10-09 01:59:45 +0800437
Patrick Williams5d82f472022-07-22 19:26:53 -0500438 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Yong Li18d77262018-10-09 01:59:45 +0800439
Yong Li18d77262018-10-09 01:59:45 +0800440 try
441 {
442 auto acpiObject = ipmi::getDbusObject(bus, acpi_state::acpiInterface);
443
444 auto sysACPIVal = ipmi::getDbusProperty(
445 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
446 acpi_state::sysACPIProp);
447 auto sysACPI = acpi_state::ACPIPowerState::convertACPIFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -0700448 std::get<std::string>(sysACPIVal));
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000449 sysAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(sysACPI));
Yong Li18d77262018-10-09 01:59:45 +0800450
451 auto devACPIVal = ipmi::getDbusProperty(
452 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
453 acpi_state::devACPIProp);
454 auto devACPI = acpi_state::ACPIPowerState::convertACPIFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -0700455 std::get<std::string>(devACPIVal));
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000456 devAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(devACPI));
Yong Li18d77262018-10-09 01:59:45 +0800457 }
458 catch (const InternalFailure& e)
459 {
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000460 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800461 }
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000462
463 return ipmi::responseSuccess(sysAcpiState, devAcpiState);
Chris Austen6caf28b2015-10-13 12:40:40 -0500464}
465
Chris Austen7303bdc2016-04-17 11:50:54 -0500466typedef struct
467{
468 char major;
469 char minor;
Potin Laic7c55922023-02-16 10:33:55 +0800470 uint8_t aux[4];
Vernon Mauery86a50822019-03-25 13:11:36 -0700471} Revision;
Chris Austen7303bdc2016-04-17 11:50:54 -0500472
Potin Lai5d214202023-01-16 18:11:49 +0800473/* Use regular expression searching matched pattern X.Y, and convert it to */
474/* Major (X) and Minor (Y) version. */
475/* Example: */
476/* version = 2.14.0-dev */
477/* ^ ^ */
478/* | |---------------- Minor */
479/* |------------------ Major */
480/* */
Potin Laic7c55922023-02-16 10:33:55 +0800481/* Default regex string only tries to match Major and Minor version. */
482/* */
483/* To match more firmware version info, platforms need to define it own */
484/* regex string to match more strings, and assign correct mapping index in */
485/* matches array. */
486/* */
487/* matches[0]: matched index for major ver */
488/* matches[1]: matched index for minor ver */
489/* matches[2]: matched index for aux[0] (set 0 to skip) */
490/* matches[3]: matched index for aux[1] (set 0 to skip) */
491/* matches[4]: matched index for aux[2] (set 0 to skip) */
492/* matches[5]: matched index for aux[3] (set 0 to skip) */
493/* Example: */
494/* regex = "([\d]+).([\d]+).([\d]+)-dev-([\d]+)-g([0-9a-fA-F]{2}) */
495/* ([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})" */
496/* matches = {1,2,5,6,7,8} */
497/* version = 2.14.0-dev-750-g37a7c5ad1-dirty */
498/* ^ ^ ^ ^ ^ ^ ^ ^ */
499/* | | | | | | | | */
500/* | | | | | | | |-- Aux byte 3 (0xAD), index 8 */
501/* | | | | | | |---- Aux byte 2 (0xC5), index 7 */
502/* | | | | | |------ Aux byte 1 (0xA7), index 6 */
503/* | | | | |-------- Aux byte 0 (0x37), index 5 */
504/* | | | |------------- Not used, index 4 */
505/* | | |------------------- Not used, index 3 */
506/* | |---------------------- Minor (14), index 2 */
507/* |------------------------ Major (2), index 1 */
Potin Lai5d214202023-01-16 18:11:49 +0800508int convertVersion(std::string s, Revision& rev)
Chris Austen7303bdc2016-04-17 11:50:54 -0500509{
Potin Laic7c55922023-02-16 10:33:55 +0800510 static const std::vector<size_t> matches = {
511 MAJOR_MATCH_INDEX, MINOR_MATCH_INDEX, AUX_0_MATCH_INDEX,
512 AUX_1_MATCH_INDEX, AUX_2_MATCH_INDEX, AUX_3_MATCH_INDEX};
Potin Lai5d214202023-01-16 18:11:49 +0800513 std::regex fw_regex(FW_VER_REGEX);
514 std::smatch m;
515 Revision r = {0};
516 size_t val;
Chris Austen7303bdc2016-04-17 11:50:54 -0500517
Potin Laidcde04e2023-03-16 18:44:15 +0800518 if (std::regex_search(s, m, fw_regex))
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600519 {
Potin Laic7c55922023-02-16 10:33:55 +0800520 if (m.size() < *std::max_element(matches.begin(), matches.end()))
521 { // max index higher than match count
Potin Laidcde04e2023-03-16 18:44:15 +0800522 return -1;
523 }
524
Potin Lai5d214202023-01-16 18:11:49 +0800525 // convert major
526 {
Potin Laic7c55922023-02-16 10:33:55 +0800527 std::string_view str = m[matches[0]].str();
Potin Lai5d214202023-01-16 18:11:49 +0800528 auto [ptr, ec]{std::from_chars(str.begin(), str.end(), val)};
529 if (ec != std::errc() || ptr != str.begin() + str.size())
530 { // failed to convert major string
Potin Laidcde04e2023-03-16 18:44:15 +0800531 return -1;
Potin Lai5d214202023-01-16 18:11:49 +0800532 }
Potin Lai058e2912023-09-07 10:16:38 +0800533
534 if (val >= 2000)
535 { // For the platforms use year as major version, it would expect to
536 // have major version between 0 - 99. If the major version is
537 // greater than or equal to 2000, it is treated as a year and
538 // converted to 0 - 99.
539 r.major = val % 100;
540 }
541 else
542 {
543 r.major = val & 0x7F;
544 }
Potin Lai5d214202023-01-16 18:11:49 +0800545 }
546
547 // convert minor
548 {
Potin Laic7c55922023-02-16 10:33:55 +0800549 std::string_view str = m[matches[1]].str();
Potin Lai5d214202023-01-16 18:11:49 +0800550 auto [ptr, ec]{std::from_chars(str.begin(), str.end(), val)};
551 if (ec != std::errc() || ptr != str.begin() + str.size())
552 { // failed to convert minor string
Potin Laidcde04e2023-03-16 18:44:15 +0800553 return -1;
Potin Lai5d214202023-01-16 18:11:49 +0800554 }
555 r.minor = val & 0xFF;
556 }
557
Potin Laic7c55922023-02-16 10:33:55 +0800558 // convert aux bytes
559 {
560 size_t i;
561 for (i = 0; i < 4; i++)
562 {
563 if (matches[i + 2] == 0)
564 {
565 continue;
566 }
567
568 std::string_view str = m[matches[i + 2]].str();
569 auto [ptr,
570 ec]{std::from_chars(str.begin(), str.end(), val, 16)};
571 if (ec != std::errc() || ptr != str.begin() + str.size())
572 { // failed to convert aux byte string
573 break;
574 }
575
576 r.aux[i] = val & 0xFF;
577 }
578
579 if (i != 4)
580 { // something wrong durign converting aux bytes
581 return -1;
582 }
583 }
584
Potin Lai5d214202023-01-16 18:11:49 +0800585 // all matched
586 rev = r;
587 return 0;
Chris Austen176c9652016-04-30 16:32:17 -0500588 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500589
Potin Lai5d214202023-01-16 18:11:49 +0800590 return -1;
Chris Austen7303bdc2016-04-17 11:50:54 -0500591}
592
Vernon Maueryea1c4012019-05-24 13:26:16 -0700593/* @brief: Implement the Get Device ID IPMI command per the IPMI spec
594 * @param[in] ctx - shared_ptr to an IPMI context struct
595 *
596 * @returns IPMI completion code plus response data
597 * - Device ID (manufacturer defined)
598 * - Device revision[4 bits]; reserved[3 bits]; SDR support[1 bit]
599 * - FW revision major[7 bits] (binary encoded); available[1 bit]
600 * - FW Revision minor (BCD encoded)
601 * - IPMI version (0x02 for IPMI 2.0)
602 * - device support (bitfield of supported options)
603 * - MFG IANA ID (3 bytes)
604 * - product ID (2 bytes)
605 * - AUX info (4 bytes)
606 */
607ipmi::RspType<uint8_t, // Device ID
608 uint8_t, // Device Revision
609 uint8_t, // Firmware Revision Major
610 uint8_t, // Firmware Revision minor
611 uint8_t, // IPMI version
612 uint8_t, // Additional device support
613 uint24_t, // MFG ID
614 uint16_t, // Product ID
615 uint32_t // AUX info
616 >
Willy Tu11d68892022-01-20 10:37:34 -0800617 ipmiAppGetDeviceId([[maybe_unused]] ipmi::Context::ptr ctx)
Chris Austen6caf28b2015-10-13 12:40:40 -0500618{
Vernon Mauery86a50822019-03-25 13:11:36 -0700619 static struct
620 {
621 uint8_t id;
622 uint8_t revision;
623 uint8_t fw[2];
624 uint8_t ipmiVer;
625 uint8_t addnDevSupport;
626 uint24_t manufId;
627 uint16_t prodId;
628 uint32_t aux;
629 } devId;
David Cobbleya1adb072017-11-21 15:58:13 -0800630 static bool dev_id_initialized = false;
Patrick Venture94930a12019-04-30 10:01:58 -0700631 static bool defaultActivationSetting = true;
David Cobbleya1adb072017-11-21 15:58:13 -0800632 const char* filename = "/usr/share/ipmi-providers/dev_id.json";
Alexander Amelkinba19c182018-09-04 15:49:36 +0300633 constexpr auto ipmiDevIdStateShift = 7;
634 constexpr auto ipmiDevIdFw1Mask = ~(1 << ipmiDevIdStateShift);
Willy Tub78184e2022-10-27 22:57:38 +0000635
636#ifdef GET_DBUS_ACTIVE_SOFTWARE
637 static bool haveBMCVersion = false;
JeffLin27a62ec2021-11-24 15:40:33 +0800638 if (!haveBMCVersion || !dev_id_initialized)
David Cobbleya1adb072017-11-21 15:58:13 -0800639 {
Willy Tub78184e2022-10-27 22:57:38 +0000640 int r = -1;
641 Revision rev = {0, 0, 0, 0};
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600642 try
643 {
Vernon Maueryea1c4012019-05-24 13:26:16 -0700644 auto version = getActiveSoftwareVersionInfo(ctx);
Vernon Mauery86a50822019-03-25 13:11:36 -0700645 r = convertVersion(version, rev);
David Cobbleya1adb072017-11-21 15:58:13 -0800646 }
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600647 catch (const std::exception& e)
648 {
649 log<level::ERR>(e.what());
650 }
Nan Liee0cb902016-07-11 15:38:03 +0800651
Patrick Venture0b02be92018-08-31 11:55:55 -0700652 if (r >= 0)
653 {
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600654 // bit7 identifies if the device is available
655 // 0=normal operation
656 // 1=device firmware, SDR update,
657 // or self-initialization in progress.
Alexander Amelkinba19c182018-09-04 15:49:36 +0300658 // The availability may change in run time, so mask here
659 // and initialize later.
Vernon Mauery86a50822019-03-25 13:11:36 -0700660 devId.fw[0] = rev.major & ipmiDevIdFw1Mask;
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600661
662 rev.minor = (rev.minor > 99 ? 99 : rev.minor);
Vernon Mauery86a50822019-03-25 13:11:36 -0700663 devId.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16;
Potin Laic7c55922023-02-16 10:33:55 +0800664 std::memcpy(&devId.aux, rev.aux, sizeof(rev.aux));
Brandon Kimc1f5aca2022-03-17 17:54:06 -0700665 haveBMCVersion = true;
David Cobbleya1adb072017-11-21 15:58:13 -0800666 }
JeffLin27a62ec2021-11-24 15:40:33 +0800667 }
Willy Tub78184e2022-10-27 22:57:38 +0000668#endif
JeffLin27a62ec2021-11-24 15:40:33 +0800669 if (!dev_id_initialized)
670 {
David Cobbleya1adb072017-11-21 15:58:13 -0800671 // IPMI Spec version 2.0
Vernon Mauery86a50822019-03-25 13:11:36 -0700672 devId.ipmiVer = 2;
Adriana Kobylak0e912642016-06-22 16:54:39 -0500673
Vernon Mauery86a50822019-03-25 13:11:36 -0700674 std::ifstream devIdFile(filename);
675 if (devIdFile.is_open())
David Cobbleya1adb072017-11-21 15:58:13 -0800676 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700677 auto data = nlohmann::json::parse(devIdFile, nullptr, false);
David Cobbleya1adb072017-11-21 15:58:13 -0800678 if (!data.is_discarded())
679 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700680 devId.id = data.value("id", 0);
681 devId.revision = data.value("revision", 0);
682 devId.addnDevSupport = data.value("addn_dev_support", 0);
683 devId.manufId = data.value("manuf_id", 0);
684 devId.prodId = data.value("prod_id", 0);
Potin Lai9a686362023-09-05 09:27:53 +0800685#ifdef GET_DBUS_ACTIVE_SOFTWARE
686 if (!(AUX_0_MATCH_INDEX || AUX_1_MATCH_INDEX ||
687 AUX_2_MATCH_INDEX || AUX_3_MATCH_INDEX))
688#endif
689 {
690 devId.aux = data.value("aux", 0);
691 }
David Cobbleya1adb072017-11-21 15:58:13 -0800692
Willy Tubfd3a172022-05-31 13:57:54 -0700693 if (data.contains("firmware_revision"))
694 {
695 const auto& firmwareRevision = data.at("firmware_revision");
696 if (firmwareRevision.contains("major"))
697 {
698 firmwareRevision.at("major").get_to(devId.fw[0]);
699 }
700 if (firmwareRevision.contains("minor"))
701 {
702 firmwareRevision.at("minor").get_to(devId.fw[1]);
703 }
704 }
705
Patrick Venture94930a12019-04-30 10:01:58 -0700706 // Set the availablitity of the BMC.
707 defaultActivationSetting = data.value("availability", true);
708
Patrick Venture0b02be92018-08-31 11:55:55 -0700709 // Don't read the file every time if successful
David Cobbleya1adb072017-11-21 15:58:13 -0800710 dev_id_initialized = true;
711 }
712 else
713 {
714 log<level::ERR>("Device ID JSON parser failure");
Vernon Maueryf2587fc2019-04-09 14:28:15 -0700715 return ipmi::responseUnspecifiedError();
David Cobbleya1adb072017-11-21 15:58:13 -0800716 }
717 }
718 else
719 {
720 log<level::ERR>("Device ID file not found");
Vernon Maueryf2587fc2019-04-09 14:28:15 -0700721 return ipmi::responseUnspecifiedError();
Chris Austen7303bdc2016-04-17 11:50:54 -0500722 }
723 }
Chris Austen6caf28b2015-10-13 12:40:40 -0500724
Alexander Amelkinba19c182018-09-04 15:49:36 +0300725 // Set availability to the actual current BMC state
Vernon Mauery86a50822019-03-25 13:11:36 -0700726 devId.fw[0] &= ipmiDevIdFw1Mask;
Patrick Venture94930a12019-04-30 10:01:58 -0700727 if (!getCurrentBmcStateWithFallback(defaultActivationSetting))
Alexander Amelkinba19c182018-09-04 15:49:36 +0300728 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700729 devId.fw[0] |= (1 << ipmiDevIdStateShift);
Alexander Amelkinba19c182018-09-04 15:49:36 +0300730 }
731
Vernon Mauery86a50822019-03-25 13:11:36 -0700732 return ipmi::responseSuccess(
733 devId.id, devId.revision, devId.fw[0], devId.fw[1], devId.ipmiVer,
734 devId.addnDevSupport, devId.manufId, devId.prodId, devId.aux);
Chris Austen6caf28b2015-10-13 12:40:40 -0500735}
736
Vernon Maueryb84a5282019-03-25 13:39:03 -0700737auto ipmiAppGetSelfTestResults() -> ipmi::RspType<uint8_t, uint8_t>
Nan Li41fa24a2016-11-10 20:12:37 +0800738{
Nan Li41fa24a2016-11-10 20:12:37 +0800739 // Byte 2:
740 // 55h - No error.
Gunnar Mills8991dd62017-10-25 17:11:29 -0500741 // 56h - Self Test function not implemented in this controller.
Nan Li41fa24a2016-11-10 20:12:37 +0800742 // 57h - Corrupted or inaccesssible data or devices.
743 // 58h - Fatal hardware error.
744 // FFh - reserved.
745 // all other: Device-specific 'internal failure'.
746 // Byte 3:
747 // For byte 2 = 55h, 56h, FFh: 00h
748 // For byte 2 = 58h, all other: Device-specific
749 // For byte 2 = 57h: self-test error bitfield.
750 // Note: returning 57h does not imply that all test were run.
751 // [7] 1b = Cannot access SEL device.
752 // [6] 1b = Cannot access SDR Repository.
753 // [5] 1b = Cannot access BMC FRU device.
754 // [4] 1b = IPMB signal lines do not respond.
755 // [3] 1b = SDR Repository empty.
756 // [2] 1b = Internal Use Area of BMC FRU corrupted.
757 // [1] 1b = controller update 'boot block' firmware corrupted.
758 // [0] 1b = controller operational firmware corrupted.
Vernon Maueryb84a5282019-03-25 13:39:03 -0700759 constexpr uint8_t notImplemented = 0x56;
760 constexpr uint8_t zero = 0;
761 return ipmi::responseSuccess(notImplemented, zero);
Nan Li41fa24a2016-11-10 20:12:37 +0800762}
763
Vernon Mauery15541322019-03-25 13:33:03 -0700764static constexpr size_t uuidBinaryLength = 16;
765static std::array<uint8_t, uuidBinaryLength> rfc4122ToIpmi(std::string rfc4122)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500766{
Willy Tu523e2d12023-09-05 11:36:48 -0700767 using Argument = xyz::openbmc_project::common::InvalidArgument;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500768 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
Patrick Ventured2117022018-02-06 08:54:37 -0800769 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte
770 // order
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500771 // Ex: 0x2332fc2c40e66298e511f2782395a361
Vernon Mauery15541322019-03-25 13:33:03 -0700772 constexpr size_t uuidHexLength = (2 * uuidBinaryLength);
773 constexpr size_t uuidRfc4122Length = (uuidHexLength + 4);
774 std::array<uint8_t, uuidBinaryLength> uuid;
775 if (rfc4122.size() == uuidRfc4122Length)
Patrick Venture0b02be92018-08-31 11:55:55 -0700776 {
Vernon Mauery15541322019-03-25 13:33:03 -0700777 rfc4122.erase(std::remove(rfc4122.begin(), rfc4122.end(), '-'),
778 rfc4122.end());
Sergey Solomineb9b8142016-08-23 09:07:28 -0500779 }
Vernon Mauery15541322019-03-25 13:33:03 -0700780 if (rfc4122.size() != uuidHexLength)
vishwa1eaea4f2016-02-26 11:57:40 -0600781 {
Vernon Mauery15541322019-03-25 13:33:03 -0700782 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
783 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
vishwa1eaea4f2016-02-26 11:57:40 -0600784 }
Vernon Mauery15541322019-03-25 13:33:03 -0700785 for (size_t ind = 0; ind < uuidHexLength; ind += 2)
vishwa1eaea4f2016-02-26 11:57:40 -0600786 {
Vernon Mauery15541322019-03-25 13:33:03 -0700787 char v[3];
788 v[0] = rfc4122[ind];
789 v[1] = rfc4122[ind + 1];
790 v[2] = 0;
791 size_t err;
792 long b;
793 try
Emily Shafferedb8bb02018-09-27 14:50:15 -0700794 {
Vernon Mauery15541322019-03-25 13:33:03 -0700795 b = std::stoul(v, &err, 16);
Emily Shafferedb8bb02018-09-27 14:50:15 -0700796 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500797 catch (const std::exception& e)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500798 {
Vernon Mauery15541322019-03-25 13:33:03 -0700799 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
800 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500801 }
Vernon Mauery15541322019-03-25 13:33:03 -0700802 // check that exactly two ascii bytes were converted
803 if (err != 2)
804 {
805 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
806 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
807 }
808 uuid[uuidBinaryLength - (ind / 2) - 1] = static_cast<uint8_t>(b);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500809 }
Vernon Mauery15541322019-03-25 13:33:03 -0700810 return uuid;
811}
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500812
Vernon Mauery15541322019-03-25 13:33:03 -0700813auto ipmiAppGetDeviceGuid()
814 -> ipmi::RspType<std::array<uint8_t, uuidBinaryLength>>
815{
816 // return a fixed GUID based on /etc/machine-id
817 // This should match the /redfish/v1/Managers/bmc's UUID data
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500818
Vernon Mauery15541322019-03-25 13:33:03 -0700819 // machine specific application ID (for BMC ID)
820 // generated by systemd-id128 -p new as per man page
821 static constexpr sd_id128_t bmcUuidAppId = SD_ID128_MAKE(
822 e0, e1, 73, 76, 64, 61, 47, da, a5, 0c, d0, cc, 64, 12, 45, 78);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500823
Vernon Mauery15541322019-03-25 13:33:03 -0700824 sd_id128_t bmcUuid;
825 // create the UUID from /etc/machine-id via the systemd API
826 sd_id128_get_machine_app_specific(bmcUuidAppId, &bmcUuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500827
Vernon Mauery15541322019-03-25 13:33:03 -0700828 char bmcUuidCstr[SD_ID128_STRING_MAX];
829 std::string systemUuid = sd_id128_to_string(bmcUuid, bmcUuidCstr);
830
831 std::array<uint8_t, uuidBinaryLength> uuid = rfc4122ToIpmi(systemUuid);
832 return ipmi::responseSuccess(uuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500833}
Chris Austen6caf28b2015-10-13 12:40:40 -0500834
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700835auto ipmiAppGetBtCapabilities()
836 -> ipmi::RspType<uint8_t, uint8_t, uint8_t, uint8_t, uint8_t>
vishwabmcba0bd5f2015-09-30 16:50:23 +0530837{
Adriana Kobylak88ad8152016-12-13 10:09:08 -0600838 // Per IPMI 2.0 spec, the input and output buffer size must be the max
839 // buffer size minus one byte to allocate space for the length byte.
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700840 constexpr uint8_t nrOutstanding = 0x01;
841 constexpr uint8_t inputBufferSize = MAX_IPMI_BUFFER - 1;
842 constexpr uint8_t outputBufferSize = MAX_IPMI_BUFFER - 1;
843 constexpr uint8_t transactionTime = 0x0A;
844 constexpr uint8_t nrRetries = 0x01;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530845
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700846 return ipmi::responseSuccess(nrOutstanding, inputBufferSize,
847 outputBufferSize, transactionTime, nrRetries);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530848}
849
Vernon Mauery6c44a942023-07-27 11:00:10 -0700850auto ipmiAppGetSystemGuid(ipmi::Context::ptr& ctx)
851 -> ipmi::RspType<std::array<uint8_t, 16>>
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600852{
Vernon Maueryb90a5322019-03-25 13:36:55 -0700853 static constexpr auto uuidInterface = "xyz.openbmc_project.Common.UUID";
854 static constexpr auto uuidProperty = "UUID";
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600855
Vernon Mauery6c44a942023-07-27 11:00:10 -0700856 // Get the Inventory object implementing BMC interface
857 ipmi::DbusObjectInfo objectInfo{};
858 boost::system::error_code ec = ipmi::getDbusObject(ctx, uuidInterface,
859 objectInfo);
860 if (ec.value())
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600861 {
Vernon Mauery6c44a942023-07-27 11:00:10 -0700862 log<level::ERR>("Failed to locate System UUID object",
863 entry("INTERFACE=%s", uuidInterface),
864 entry("ERROR=%s", ec.message().c_str()));
865 return ipmi::responseUnspecifiedError();
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600866 }
Vernon Mauery6c44a942023-07-27 11:00:10 -0700867
868 // Read UUID property value from bmcObject
869 // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
870 std::string rfc4122Uuid{};
871 ec = ipmi::getDbusProperty(ctx, objectInfo.second, objectInfo.first,
872 uuidInterface, uuidProperty, rfc4122Uuid);
873 if (ec.value())
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600874 {
875 log<level::ERR>("Failed in reading BMC UUID property",
Vernon Maueryb90a5322019-03-25 13:36:55 -0700876 entry("INTERFACE=%s", uuidInterface),
Vernon Mauery6c44a942023-07-27 11:00:10 -0700877 entry("PROPERTY=%s", uuidProperty),
878 entry("ERROR=%s", ec.message().c_str()));
Vernon Maueryb90a5322019-03-25 13:36:55 -0700879 return ipmi::responseUnspecifiedError();
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600880 }
Vernon Maueryb90a5322019-03-25 13:36:55 -0700881 std::array<uint8_t, 16> uuid;
Vernon Maueryb90a5322019-03-25 13:36:55 -0700882 try
883 {
884 // convert to IPMI format
885 uuid = rfc4122ToIpmi(rfc4122Uuid);
886 }
887 catch (const InvalidArgument& e)
888 {
889 log<level::ERR>("Failed in parsing BMC UUID property",
890 entry("INTERFACE=%s", uuidInterface),
891 entry("PROPERTY=%s", uuidProperty),
892 entry("VALUE=%s", rfc4122Uuid.c_str()));
893 return ipmi::responseUnspecifiedError();
894 }
895 return ipmi::responseSuccess(uuid);
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600896}
897
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000898/**
899 * @brief set the session state as teardown
900 *
901 * This function is to set the session state to tear down in progress if the
902 * state is active.
903 *
904 * @param[in] busp - Dbus obj
905 * @param[in] service - service name
906 * @param[in] obj - object path
907 *
908 * @return success completion code if it sets the session state to
909 * tearDownInProgress else return the corresponding error completion code.
910 **/
911uint8_t setSessionState(std::shared_ptr<sdbusplus::asio::connection>& busp,
912 const std::string& service, const std::string& obj)
913{
914 try
915 {
916 uint8_t sessionState = std::get<uint8_t>(ipmi::getDbusProperty(
917 *busp, service, obj, session::sessionIntf, "State"));
918
919 if (sessionState == static_cast<uint8_t>(session::State::active))
920 {
921 ipmi::setDbusProperty(
922 *busp, service, obj, session::sessionIntf, "State",
923 static_cast<uint8_t>(session::State::tearDownInProgress));
924 return ipmi::ccSuccess;
925 }
926 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500927 catch (const std::exception& e)
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000928 {
929 log<level::ERR>("Failed in getting session state property",
930 entry("service=%s", service.c_str()),
931 entry("object path=%s", obj.c_str()),
932 entry("interface=%s", session::sessionIntf));
933 return ipmi::ccUnspecifiedError;
934 }
935
936 return ipmi::ccInvalidFieldRequest;
937}
938
939ipmi::RspType<> ipmiAppCloseSession(uint32_t reqSessionId,
940 std::optional<uint8_t> requestSessionHandle)
941{
942 auto busp = getSdBus();
943 uint8_t reqSessionHandle =
944 requestSessionHandle.value_or(session::defaultSessionHandle);
945
946 if (reqSessionId == session::sessionZero &&
947 reqSessionHandle == session::defaultSessionHandle)
948 {
949 return ipmi::response(session::ccInvalidSessionId);
950 }
951
952 if (reqSessionId == session::sessionZero &&
953 reqSessionHandle == session::invalidSessionHandle)
954 {
955 return ipmi::response(session::ccInvalidSessionHandle);
956 }
957
958 if (reqSessionId != session::sessionZero &&
959 reqSessionHandle != session::defaultSessionHandle)
960 {
961 return ipmi::response(ipmi::ccInvalidFieldRequest);
962 }
963
964 try
965 {
966 ipmi::ObjectTree objectTree = ipmi::getAllDbusObjects(
967 *busp, session::sessionManagerRootPath, session::sessionIntf);
968
969 for (auto& objectTreeItr : objectTree)
970 {
971 const std::string obj = objectTreeItr.first;
972
973 if (isSessionObjectMatched(obj, reqSessionId, reqSessionHandle))
974 {
975 auto& serviceMap = objectTreeItr.second;
976
977 // Session id and session handle are unique for each session.
978 // Session id and handler are retrived from the object path and
979 // object path will be unique for each session. Checking if
980 // multiple objects exist with same object path under multiple
981 // services.
982 if (serviceMap.size() != 1)
983 {
984 return ipmi::responseUnspecifiedError();
985 }
986
987 auto itr = serviceMap.begin();
988 const std::string service = itr->first;
989 return ipmi::response(setSessionState(busp, service, obj));
990 }
991 }
992 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500993 catch (const sdbusplus::exception_t& e)
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000994 {
995 log<level::ERR>("Failed to fetch object from dbus",
996 entry("INTERFACE=%s", session::sessionIntf),
997 entry("ERRMSG=%s", e.what()));
998 return ipmi::responseUnspecifiedError();
999 }
1000
1001 return ipmi::responseInvalidFieldRequest();
1002}
1003
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001004uint8_t getTotalSessionCount()
1005{
Meera-Kattac1789482021-05-18 09:53:26 +00001006 uint8_t count = 0, ch = 0;
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001007
1008 while (ch < ipmi::maxIpmiChannels &&
1009 count < session::maxNetworkInstanceSupported)
1010 {
Jayaprakash Mutyala80207e62020-07-04 16:34:15 +00001011 ipmi::ChannelInfo chInfo{};
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001012 ipmi::getChannelInfo(ch, chInfo);
1013 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) ==
1014 ipmi::EChannelMediumType::lan8032)
1015 {
1016 count++;
1017 }
1018 ch++;
1019 }
1020 return count * session::maxSessionCountPerChannel;
1021}
1022
1023/**
1024 * @brief get session info request data.
1025 *
1026 * This function validates the request data and retrive request session id,
1027 * session handle.
1028 *
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301029 * @param[in] ctx - context of current session.
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001030 * @param[in] sessionIndex - request session index
1031 * @param[in] payload - input payload
1032 * @param[in] reqSessionId - unpacked session Id will be asigned
1033 * @param[in] reqSessionHandle - unpacked session handle will be asigned
1034 *
1035 * @return success completion code if request data is valid
1036 * else return the correcponding error completion code.
1037 **/
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301038uint8_t getSessionInfoRequestData(const ipmi::Context::ptr ctx,
1039 const uint8_t sessionIndex,
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001040 ipmi::message::Payload& payload,
1041 uint32_t& reqSessionId,
1042 uint8_t& reqSessionHandle)
1043{
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301044 if ((sessionIndex > session::maxSessionCountPerChannel) &&
1045 (sessionIndex < session::searchSessionByHandle))
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001046 {
1047 return ipmi::ccInvalidFieldRequest;
1048 }
1049
1050 switch (sessionIndex)
1051 {
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301052 case session::searchCurrentSession:
1053
1054 ipmi::ChannelInfo chInfo;
1055 ipmi::getChannelInfo(ctx->channel, chInfo);
1056
1057 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) !=
1058 ipmi::EChannelMediumType::lan8032)
1059 {
1060 return ipmi::ccInvalidFieldRequest;
1061 }
1062
1063 if (!payload.fullyUnpacked())
1064 {
1065 return ipmi::ccReqDataLenInvalid;
1066 }
1067 // Check if current sessionId is 0, sessionId 0 is reserved.
1068 if (ctx->sessionId == session::sessionZero)
1069 {
1070 return session::ccInvalidSessionId;
1071 }
1072 reqSessionId = ctx->sessionId;
1073 break;
1074
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001075 case session::searchSessionByHandle:
1076
1077 if ((payload.unpack(reqSessionHandle)) ||
1078 (!payload.fullyUnpacked()))
1079 {
1080 return ipmi::ccReqDataLenInvalid;
1081 }
1082
1083 if ((reqSessionHandle == session::sessionZero) ||
1084 ((reqSessionHandle & session::multiIntfaceSessionHandleMask) >
1085 session::maxSessionCountPerChannel))
1086 {
1087 return session::ccInvalidSessionHandle;
1088 }
1089 break;
1090
1091 case session::searchSessionById:
1092
1093 if ((payload.unpack(reqSessionId)) || (!payload.fullyUnpacked()))
1094 {
1095 return ipmi::ccReqDataLenInvalid;
1096 }
1097
1098 if (reqSessionId == session::sessionZero)
1099 {
1100 return session::ccInvalidSessionId;
1101 }
1102 break;
1103
1104 default:
1105 if (!payload.fullyUnpacked())
1106 {
1107 return ipmi::ccReqDataLenInvalid;
1108 }
1109 break;
1110 }
1111 return ipmi::ccSuccess;
1112}
1113
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001114uint8_t getSessionState(ipmi::Context::ptr ctx, const std::string& service,
1115 const std::string& objPath, uint8_t& sessionState)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001116{
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001117 boost::system::error_code ec = ipmi::getDbusProperty(
1118 ctx, service, objPath, session::sessionIntf, "State", sessionState);
1119 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001120 {
1121 log<level::ERR>("Failed to fetch state property ",
1122 entry("SERVICE=%s", service.c_str()),
1123 entry("OBJECTPATH=%s", objPath.c_str()),
1124 entry("INTERFACE=%s", session::sessionIntf),
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001125 entry("ERRMSG=%s", ec.message().c_str()));
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001126 return ipmi::ccUnspecifiedError;
1127 }
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001128 return ipmi::ccSuccess;
1129}
1130
1131static constexpr uint8_t macAddrLen = 6;
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001132/** Alias SessionDetails - contain the optional information about an
1133 * RMCP+ session.
1134 *
1135 * @param userID - uint6_t session user ID (0-63)
1136 * @param reserved - uint2_t reserved
1137 * @param privilege - uint4_t session privilege (0-5)
1138 * @param reserved - uint4_t reserved
1139 * @param channel - uint4_t session channel number
1140 * @param protocol - uint4_t session protocol
1141 * @param remoteIP - uint32_t remote IP address
1142 * @param macAddr - std::array<uint8_t, 6> mac address
1143 * @param port - uint16_t remote port
1144 */
1145using SessionDetails =
1146 std::tuple<uint2_t, uint6_t, uint4_t, uint4_t, uint4_t, uint4_t, uint32_t,
1147 std::array<uint8_t, macAddrLen>, uint16_t>;
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001148
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001149/** @brief get session details for a given session
1150 *
1151 * @param[in] ctx - ipmi::Context pointer for accessing D-Bus
1152 * @param[in] service - D-Bus service name to fetch details from
1153 * @param[in] objPath - D-Bus object path for session
1154 * @param[out] sessionHandle - return session handle for session
1155 * @param[out] sessionState - return session state for session
1156 * @param[out] details - return a SessionDetails tuple containing other
1157 * session info
1158 * @return - ipmi::Cc success or error code
1159 */
1160ipmi::Cc getSessionDetails(ipmi::Context::ptr ctx, const std::string& service,
1161 const std::string& objPath, uint8_t& sessionHandle,
1162 uint8_t& sessionState, SessionDetails& details)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001163{
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001164 ipmi::PropertyMap sessionProps;
1165 boost::system::error_code ec = ipmi::getAllDbusProperties(
1166 ctx, service, objPath, session::sessionIntf, sessionProps);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001167
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001168 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001169 {
1170 log<level::ERR>("Failed to fetch state property ",
1171 entry("SERVICE=%s", service.c_str()),
1172 entry("OBJECTPATH=%s", objPath.c_str()),
1173 entry("INTERFACE=%s", session::sessionIntf),
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001174 entry("ERRMSG=%s", ec.message().c_str()));
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001175 return ipmi::ccUnspecifiedError;
1176 }
1177
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001178 sessionState = ipmi::mappedVariant<uint8_t>(
1179 sessionProps, "State", static_cast<uint8_t>(session::State::inactive));
1180 if (sessionState == static_cast<uint8_t>(session::State::active))
1181 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001182 sessionHandle = ipmi::mappedVariant<uint8_t>(sessionProps,
1183 "SessionHandle", 0);
1184 std::get<0>(details) = ipmi::mappedVariant<uint8_t>(sessionProps,
1185 "UserID", 0xff);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001186 // std::get<1>(details) = 0; // (default constructed to 0)
1187 std::get<2>(details) =
1188 ipmi::mappedVariant<uint8_t>(sessionProps, "CurrentPrivilege", 0);
1189 // std::get<3>(details) = 0; // (default constructed to 0)
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001190 std::get<4>(details) = ipmi::mappedVariant<uint8_t>(sessionProps,
1191 "ChannelNum", 0xff);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001192 constexpr uint4_t rmcpPlusProtocol = 1;
1193 std::get<5>(details) = rmcpPlusProtocol;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001194 std::get<6>(details) = ipmi::mappedVariant<uint32_t>(sessionProps,
1195 "RemoteIPAddr", 0);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001196 // std::get<7>(details) = {{0}}; // default constructed to all 0
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001197 std::get<8>(details) = ipmi::mappedVariant<uint16_t>(sessionProps,
1198 "RemotePort", 0);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001199 }
1200
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001201 return ipmi::ccSuccess;
1202}
1203
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001204ipmi::RspType<uint8_t, // session handle,
1205 uint8_t, // total session count
1206 uint8_t, // active session count
1207 std::optional<SessionDetails>>
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301208 ipmiAppGetSessionInfo(ipmi::Context::ptr ctx, uint8_t sessionIndex,
1209 ipmi::message::Payload& payload)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001210{
1211 uint32_t reqSessionId = 0;
1212 uint8_t reqSessionHandle = session::defaultSessionHandle;
1213 // initializing state to 0xff as 0 represents state as inactive.
1214 uint8_t state = 0xFF;
1215
1216 uint8_t completionCode = getSessionInfoRequestData(
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301217 ctx, sessionIndex, payload, reqSessionId, reqSessionHandle);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001218
1219 if (completionCode)
1220 {
1221 return ipmi::response(completionCode);
1222 }
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001223 ipmi::ObjectTree objectTree;
1224 boost::system::error_code ec = ipmi::getAllDbusObjects(
1225 ctx, session::sessionManagerRootPath, session::sessionIntf, objectTree);
1226 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001227 {
1228 log<level::ERR>("Failed to fetch object from dbus",
1229 entry("INTERFACE=%s", session::sessionIntf),
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001230 entry("ERRMSG=%s", ec.message().c_str()));
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001231 return ipmi::responseUnspecifiedError();
1232 }
1233
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001234 uint8_t totalSessionCount = getTotalSessionCount();
1235 uint8_t activeSessionCount = 0;
1236 uint8_t sessionHandle = session::defaultSessionHandle;
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001237 uint8_t activeSessionHandle = 0;
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001238 std::optional<SessionDetails> maybeDetails;
1239 uint8_t index = 0;
1240 for (auto& objectTreeItr : objectTree)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001241 {
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001242 uint32_t sessionId = 0;
1243 std::string objectPath = objectTreeItr.first;
1244
1245 if (!parseCloseSessionInputPayload(objectPath, sessionId,
1246 sessionHandle))
1247 {
1248 continue;
1249 }
1250 index++;
1251 auto& serviceMap = objectTreeItr.second;
1252 auto itr = serviceMap.begin();
1253
1254 if (serviceMap.size() != 1)
1255 {
1256 return ipmi::responseUnspecifiedError();
1257 }
1258
1259 std::string service = itr->first;
1260 uint8_t sessionState = 0;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001261 completionCode = getSessionState(ctx, service, objectPath,
1262 sessionState);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001263 if (completionCode)
1264 {
1265 return ipmi::response(completionCode);
1266 }
1267
1268 if (sessionState == static_cast<uint8_t>(session::State::active))
1269 {
1270 activeSessionCount++;
1271 }
1272
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001273 if (index == sessionIndex || reqSessionId == sessionId ||
1274 reqSessionHandle == sessionHandle)
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001275 {
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001276 SessionDetails details{};
1277 completionCode = getSessionDetails(ctx, service, objectPath,
1278 sessionHandle, state, details);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001279
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001280 if (completionCode)
1281 {
1282 return ipmi::response(completionCode);
1283 }
1284 activeSessionHandle = sessionHandle;
1285 maybeDetails = std::move(details);
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001286 }
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001287 }
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001288
1289 if (state == static_cast<uint8_t>(session::State::active) ||
1290 state == static_cast<uint8_t>(session::State::tearDownInProgress))
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001291 {
Johnathan Manteyca93bb12022-12-02 13:33:17 -08001292 return ipmi::responseSuccess(activeSessionHandle, totalSessionCount,
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001293 activeSessionCount, maybeDetails);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001294 }
1295
1296 return ipmi::responseInvalidFieldRequest();
1297}
1298
Xo Wangf542e8b2017-08-09 15:34:16 -07001299static std::unique_ptr<SysInfoParamStore> sysInfoParamStore;
1300
Xo Wang87651332017-08-11 10:17:59 -07001301static std::string sysInfoReadSystemName()
1302{
1303 // Use the BMC hostname as the "System Name."
1304 char hostname[HOST_NAME_MAX + 1] = {};
1305 if (gethostname(hostname, HOST_NAME_MAX) != 0)
1306 {
1307 perror("System info parameter: system name");
1308 }
1309 return hostname;
1310}
1311
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001312static constexpr uint8_t paramRevision = 0x11;
1313static constexpr size_t configParameterLength = 16;
Xo Wangf542e8b2017-08-09 15:34:16 -07001314
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001315static constexpr size_t smallChunkSize = 14;
1316static constexpr size_t fullChunkSize = 16;
Jia, chunhui449f2162019-09-11 16:51:51 +08001317static constexpr uint8_t progressMask = 0x3;
krishnar410752832023-11-06 13:58:35 +05301318static constexpr uint8_t maxValidEncodingData = 0x02;
Xo Wangf542e8b2017-08-09 15:34:16 -07001319
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001320static constexpr uint8_t setComplete = 0x0;
1321static constexpr uint8_t setInProgress = 0x1;
1322static constexpr uint8_t commitWrite = 0x2;
1323static uint8_t transferStatus = setComplete;
1324
Jia, chunhui449f2162019-09-11 16:51:51 +08001325static constexpr uint8_t configDataOverhead = 2;
1326
1327// For EFI based system, 256 bytes is recommended.
1328static constexpr size_t maxBytesPerParameter = 256;
1329
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001330namespace ipmi
1331{
1332constexpr Cc ccParmNotSupported = 0x80;
Jia, chunhui449f2162019-09-11 16:51:51 +08001333constexpr Cc ccSetInProgressActive = 0x81;
1334constexpr Cc ccSystemInfoParameterSetReadOnly = 0x82;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001335
1336static inline auto responseParmNotSupported()
1337{
1338 return response(ccParmNotSupported);
Xo Wangf542e8b2017-08-09 15:34:16 -07001339}
Jia, chunhui449f2162019-09-11 16:51:51 +08001340static inline auto responseSetInProgressActive()
1341{
1342 return response(ccSetInProgressActive);
1343}
1344static inline auto responseSystemInfoParameterSetReadOnly()
1345{
1346 return response(ccSystemInfoParameterSetReadOnly);
1347}
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001348} // namespace ipmi
Xo Wangf542e8b2017-08-09 15:34:16 -07001349
Jia, chunhui449f2162019-09-11 16:51:51 +08001350ipmi::RspType<uint8_t, // Parameter revision
1351 std::optional<uint8_t>, // data1 / setSelector / ProgressStatus
1352 std::optional<std::vector<uint8_t>>> // data2-17
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001353 ipmiAppGetSystemInfo(uint7_t reserved, bool getRevision,
1354 uint8_t paramSelector, uint8_t setSelector,
1355 uint8_t BlockSelector)
Xo Wangf542e8b2017-08-09 15:34:16 -07001356{
Snehalatha Va5ae7722020-05-02 18:18:57 +00001357 if (reserved || (paramSelector >= invalidParamSelectorStart &&
1358 paramSelector <= invalidParamSelectorEnd))
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001359 {
1360 return ipmi::responseInvalidFieldRequest();
1361 }
PavanKumarIntel3771f5f2023-11-02 06:26:42 +00001362 if (paramSelector >= oemCmdStart)
Snehalatha Va5ae7722020-05-02 18:18:57 +00001363 {
1364 return ipmi::responseParmNotSupported();
1365 }
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001366 if (getRevision)
Xo Wangf542e8b2017-08-09 15:34:16 -07001367 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001368 return ipmi::responseSuccess(paramRevision, std::nullopt, std::nullopt);
Xo Wangf542e8b2017-08-09 15:34:16 -07001369 }
1370
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001371 if (paramSelector == 0)
Xo Wangf542e8b2017-08-09 15:34:16 -07001372 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001373 return ipmi::responseSuccess(paramRevision, transferStatus,
1374 std::nullopt);
Xo Wangf542e8b2017-08-09 15:34:16 -07001375 }
1376
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001377 if (BlockSelector != 0) // 00h if parameter does not require a block number
Xo Wangf542e8b2017-08-09 15:34:16 -07001378 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001379 return ipmi::responseParmNotSupported();
Xo Wangf542e8b2017-08-09 15:34:16 -07001380 }
1381
1382 if (sysInfoParamStore == nullptr)
1383 {
1384 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
Xo Wang87651332017-08-11 10:17:59 -07001385 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1386 sysInfoReadSystemName);
Xo Wangf542e8b2017-08-09 15:34:16 -07001387 }
1388
1389 // Parameters other than Set In Progress are assumed to be strings.
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001390 std::tuple<bool, std::string> ret =
1391 sysInfoParamStore->lookup(paramSelector);
1392 bool found = std::get<0>(ret);
Xo Wangf542e8b2017-08-09 15:34:16 -07001393 if (!found)
1394 {
Snehalatha Va5ae7722020-05-02 18:18:57 +00001395 return ipmi::responseSensorInvalid();
Xo Wangf542e8b2017-08-09 15:34:16 -07001396 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001397 std::string& paramString = std::get<1>(ret);
Jia, chunhui449f2162019-09-11 16:51:51 +08001398 std::vector<uint8_t> configData;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001399 size_t count = 0;
1400 if (setSelector == 0)
Jia, chunhui449f2162019-09-11 16:51:51 +08001401 { // First chunk has only 14 bytes.
1402 configData.emplace_back(0); // encoding
1403 configData.emplace_back(paramString.length()); // string length
1404 count = std::min(paramString.length(), smallChunkSize);
1405 configData.resize(count + configDataOverhead);
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001406 std::copy_n(paramString.begin(), count,
Snehalatha Va5ae7722020-05-02 18:18:57 +00001407 configData.begin() + configDataOverhead); // 14 bytes chunk
1408
1409 // Append zero's to remaining bytes
1410 if (configData.size() < configParameterLength)
1411 {
1412 std::fill_n(std::back_inserter(configData),
1413 configParameterLength - configData.size(), 0x00);
1414 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001415 }
1416 else
Xo Wangf542e8b2017-08-09 15:34:16 -07001417 {
Jia, chunhui449f2162019-09-11 16:51:51 +08001418 size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001419 if (offset >= paramString.length())
1420 {
1421 return ipmi::responseParmOutOfRange();
1422 }
Jia, chunhui449f2162019-09-11 16:51:51 +08001423 count = std::min(paramString.length() - offset, fullChunkSize);
1424 configData.resize(count);
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001425 std::copy_n(paramString.begin() + offset, count,
1426 configData.begin()); // 16 bytes chunk
Xo Wangf542e8b2017-08-09 15:34:16 -07001427 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001428 return ipmi::responseSuccess(paramRevision, setSelector, configData);
Xo Wangf542e8b2017-08-09 15:34:16 -07001429}
1430
Jia, chunhui449f2162019-09-11 16:51:51 +08001431ipmi::RspType<> ipmiAppSetSystemInfo(uint8_t paramSelector, uint8_t data1,
1432 std::vector<uint8_t> configData)
1433{
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001434 if (paramSelector >= invalidParamSelectorStart &&
1435 paramSelector <= invalidParamSelectorEnd)
1436 {
1437 return ipmi::responseInvalidFieldRequest();
1438 }
PavanKumarIntel3771f5f2023-11-02 06:26:42 +00001439 if (paramSelector >= oemCmdStart)
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001440 {
1441 return ipmi::responseParmNotSupported();
1442 }
1443
Jia, chunhui449f2162019-09-11 16:51:51 +08001444 if (paramSelector == 0)
1445 {
1446 // attempt to set the 'set in progress' value (in parameter #0)
1447 // when not in the set complete state.
1448 if ((transferStatus != setComplete) && (data1 == setInProgress))
1449 {
1450 return ipmi::responseSetInProgressActive();
1451 }
1452 // only following 2 states are supported
1453 if (data1 > setInProgress)
1454 {
1455 phosphor::logging::log<phosphor::logging::level::ERR>(
1456 "illegal SetInProgress status");
1457 return ipmi::responseInvalidFieldRequest();
1458 }
1459
1460 transferStatus = data1 & progressMask;
1461 return ipmi::responseSuccess();
1462 }
1463
1464 if (configData.size() > configParameterLength)
1465 {
1466 return ipmi::responseInvalidFieldRequest();
1467 }
1468
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001469 // Append zero's to remaining bytes
1470 if (configData.size() < configParameterLength)
1471 {
1472 fill_n(back_inserter(configData),
1473 (configParameterLength - configData.size()), 0x00);
1474 }
1475
Jia, chunhui449f2162019-09-11 16:51:51 +08001476 if (!sysInfoParamStore)
1477 {
1478 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
1479 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1480 sysInfoReadSystemName);
1481 }
1482
1483 // lookup
1484 std::tuple<bool, std::string> ret =
1485 sysInfoParamStore->lookup(paramSelector);
1486 bool found = std::get<0>(ret);
1487 std::string& paramString = std::get<1>(ret);
1488 if (!found)
1489 {
1490 // parameter does not exist. Init new
1491 paramString = "";
1492 }
1493
1494 uint8_t setSelector = data1;
1495 size_t count = 0;
krishnar410752832023-11-06 13:58:35 +05301496 if (setSelector == 0) // First chunk has only 14 bytes.
Jia, chunhui449f2162019-09-11 16:51:51 +08001497 {
krishnar410752832023-11-06 13:58:35 +05301498 uint8_t encoding = configData.at(0);
1499 if (encoding > maxValidEncodingData)
1500 {
1501 return ipmi::responseInvalidFieldRequest();
1502 }
1503
Jia, chunhui449f2162019-09-11 16:51:51 +08001504 size_t stringLen = configData.at(1); // string length
1505 // maxBytesPerParamter is 256. It will always be greater than stringLen
1506 // (unit8_t) if maxBytes changes in future, then following line is
1507 // needed.
1508 // stringLen = std::min(stringLen, maxBytesPerParameter);
1509 count = std::min(stringLen, smallChunkSize);
1510 count = std::min(count, configData.size());
1511 paramString.resize(stringLen); // reserve space
1512 std::copy_n(configData.begin() + configDataOverhead, count,
1513 paramString.begin());
1514 }
1515 else
1516 {
1517 size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
1518 if (offset >= paramString.length())
1519 {
1520 return ipmi::responseParmOutOfRange();
1521 }
1522 count = std::min(paramString.length() - offset, configData.size());
1523 std::copy_n(configData.begin(), count, paramString.begin() + offset);
1524 }
1525 sysInfoParamStore->update(paramSelector, paramString);
1526 return ipmi::responseSuccess();
1527}
1528
Yong Libd0503a2019-08-22 17:17:17 +08001529#ifdef ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301530inline std::vector<uint8_t> convertStringToData(const std::string& command)
1531{
1532 std::istringstream iss(command);
1533 std::string token;
1534 std::vector<uint8_t> dataValue;
1535 while (std::getline(iss, token, ' '))
1536 {
1537 dataValue.emplace_back(
1538 static_cast<uint8_t>(std::stoul(token, nullptr, base_16)));
1539 }
1540 return dataValue;
1541}
1542
Matt Simmering68d9d402023-11-09 14:22:11 -08001543static bool populateI2CControllerWRAllowlist()
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301544{
1545 nlohmann::json data = nullptr;
Matt Simmering68d9d402023-11-09 14:22:11 -08001546 std::ifstream jsonFile(i2cControllerWRAllowlistFile);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301547
1548 if (!jsonFile.good())
1549 {
Matt Simmering68d9d402023-11-09 14:22:11 -08001550 log<level::WARNING>(
1551 "i2c allow list file not found!",
1552 entry("FILE_NAME: %s", i2cControllerWRAllowlistFile));
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301553 return false;
1554 }
1555
1556 try
1557 {
1558 data = nlohmann::json::parse(jsonFile, nullptr, false);
1559 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001560 catch (const nlohmann::json::parse_error& e)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301561 {
Matt Simmering68d9d402023-11-09 14:22:11 -08001562 log<level::ERR>("Corrupted i2c allow list config file",
1563 entry("FILE_NAME: %s", i2cControllerWRAllowlistFile),
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301564 entry("MSG: %s", e.what()));
1565 return false;
1566 }
1567
1568 try
1569 {
1570 // Example JSON Structure format
1571 // "filters": [
1572 // {
1573 // "Description": "Allow full read - ignore first byte write value
1574 // for 0x40 to 0x4F",
1575 // "busId": "0x01",
1576 // "slaveAddr": "0x40",
1577 // "slaveAddrMask": "0x0F",
1578 // "command": "0x00",
1579 // "commandMask": "0xFF"
1580 // },
1581 // {
1582 // "Description": "Allow full read - first byte match 0x05 and
1583 // ignore second byte",
1584 // "busId": "0x01",
1585 // "slaveAddr": "0x57",
1586 // "slaveAddrMask": "0x00",
1587 // "command": "0x05 0x00",
1588 // "commandMask": "0x00 0xFF"
1589 // },]
1590
1591 nlohmann::json filters = data[filtersStr].get<nlohmann::json>();
Matt Simmering68d9d402023-11-09 14:22:11 -08001592 std::vector<i2cControllerWRAllowlist>& allowlist = getWRAllowlist();
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301593 for (const auto& it : filters.items())
1594 {
1595 nlohmann::json filter = it.value();
1596 if (filter.is_null())
1597 {
1598 log<level::ERR>(
Matt Simmering68d9d402023-11-09 14:22:11 -08001599 "Corrupted I2C controller write read allowlist config file",
1600 entry("FILE_NAME: %s", i2cControllerWRAllowlistFile));
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301601 return false;
1602 }
1603 const std::vector<uint8_t>& writeData =
1604 convertStringToData(filter[cmdStr].get<std::string>());
1605 const std::vector<uint8_t>& writeDataMask =
1606 convertStringToData(filter[cmdMaskStr].get<std::string>());
1607 if (writeDataMask.size() != writeData.size())
1608 {
Matt Simmering68d9d402023-11-09 14:22:11 -08001609 log<level::ERR>("I2C controller write read allowlist filter "
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301610 "mismatch for command & mask size");
1611 return false;
1612 }
Matt Simmering68d9d402023-11-09 14:22:11 -08001613 allowlist.push_back(
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301614 {static_cast<uint8_t>(std::stoul(
1615 filter[busIdStr].get<std::string>(), nullptr, base_16)),
1616 static_cast<uint8_t>(
Matt Simmering68d9d402023-11-09 14:22:11 -08001617 std::stoul(filter[targetAddrStr].get<std::string>(),
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301618 nullptr, base_16)),
1619 static_cast<uint8_t>(
Matt Simmering68d9d402023-11-09 14:22:11 -08001620 std::stoul(filter[targetAddrMaskStr].get<std::string>(),
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301621 nullptr, base_16)),
1622 writeData, writeDataMask});
1623 }
Matt Simmering68d9d402023-11-09 14:22:11 -08001624 if (allowlist.size() != filters.size())
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301625 {
1626 log<level::ERR>(
Matt Simmering68d9d402023-11-09 14:22:11 -08001627 "I2C controller write read allowlist filter size mismatch");
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301628 return false;
1629 }
1630 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001631 catch (const std::exception& e)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301632 {
Matt Simmering68d9d402023-11-09 14:22:11 -08001633 log<level::ERR>(
1634 "I2C controller write read allowlist unexpected exception",
1635 entry("ERROR=%s", e.what()));
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301636 return false;
1637 }
1638 return true;
1639}
1640
Matt Simmering68d9d402023-11-09 14:22:11 -08001641static inline bool isWriteDataAllowlisted(const std::vector<uint8_t>& data,
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301642 const std::vector<uint8_t>& dataMask,
1643 const std::vector<uint8_t>& writeData)
1644{
1645 std::vector<uint8_t> processedDataBuf(data.size());
1646 std::vector<uint8_t> processedReqBuf(dataMask.size());
1647 std::transform(writeData.begin(), writeData.end(), dataMask.begin(),
1648 processedReqBuf.begin(), std::bit_or<uint8_t>());
1649 std::transform(data.begin(), data.end(), dataMask.begin(),
1650 processedDataBuf.begin(), std::bit_or<uint8_t>());
1651
1652 return (processedDataBuf == processedReqBuf);
1653}
1654
Matt Simmering68d9d402023-11-09 14:22:11 -08001655static bool isCmdAllowlisted(uint8_t busId, uint8_t targetAddr,
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301656 std::vector<uint8_t>& writeData)
1657{
Matt Simmering68d9d402023-11-09 14:22:11 -08001658 std::vector<i2cControllerWRAllowlist>& allowList = getWRAllowlist();
1659 for (const auto& wlEntry : allowList)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301660 {
1661 if ((busId == wlEntry.busId) &&
Matt Simmering68d9d402023-11-09 14:22:11 -08001662 ((targetAddr | wlEntry.targetAddrMask) ==
1663 (wlEntry.targetAddr | wlEntry.targetAddrMask)))
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301664 {
1665 const std::vector<uint8_t>& dataMask = wlEntry.dataMask;
1666 // Skip as no-match, if requested write data is more than the
1667 // write data mask size
1668 if (writeData.size() > dataMask.size())
1669 {
1670 continue;
1671 }
Matt Simmering68d9d402023-11-09 14:22:11 -08001672 if (isWriteDataAllowlisted(wlEntry.data, dataMask, writeData))
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301673 {
1674 return true;
1675 }
1676 }
1677 }
1678 return false;
1679}
Yong Libd0503a2019-08-22 17:17:17 +08001680#else
Matt Simmering68d9d402023-11-09 14:22:11 -08001681static bool populateI2CControllerWRAllowlist()
Yong Libd0503a2019-08-22 17:17:17 +08001682{
1683 log<level::INFO>(
Matt Simmering68d9d402023-11-09 14:22:11 -08001684 "I2C_WHITELIST_CHECK is disabled, do not populate allowlist");
Yong Libd0503a2019-08-22 17:17:17 +08001685 return true;
1686}
1687#endif // ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301688
Matt Simmering68d9d402023-11-09 14:22:11 -08001689/** @brief implements controller write read IPMI command which can be used for
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301690 * low-level I2C/SMBus write, read or write-read access
1691 * @param isPrivateBus -to indicate private bus usage
1692 * @param busId - bus id
1693 * @param channelNum - channel number
1694 * @param reserved - skip 1 bit
Matt Simmering68d9d402023-11-09 14:22:11 -08001695 * @param targetAddr - target address
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301696 * @param read count - number of bytes to be read
1697 * @param writeData - data to be written
1698 *
1699 * @returns IPMI completion code plus response data
1700 * - readData - i2c response data
1701 */
1702ipmi::RspType<std::vector<uint8_t>>
Matt Simmering68d9d402023-11-09 14:22:11 -08001703 ipmiControllerWriteRead([[maybe_unused]] bool isPrivateBus, uint3_t busId,
1704 [[maybe_unused]] uint4_t channelNum, bool reserved,
1705 uint7_t targetAddr, uint8_t readCount,
1706 std::vector<uint8_t> writeData)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301707{
Jayaprakash Mutyalac2af98b2021-09-14 09:19:11 +00001708 if (reserved)
1709 {
1710 return ipmi::responseInvalidFieldRequest();
1711 }
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301712 const size_t writeCount = writeData.size();
1713 if (!readCount && !writeCount)
1714 {
Matt Simmering68d9d402023-11-09 14:22:11 -08001715 log<level::ERR>(
1716 "Controller write read command: Read & write count are 0");
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301717 return ipmi::responseInvalidFieldRequest();
1718 }
Yong Libd0503a2019-08-22 17:17:17 +08001719#ifdef ENABLE_I2C_WHITELIST_CHECK
Matt Simmering68d9d402023-11-09 14:22:11 -08001720 if (!isCmdAllowlisted(static_cast<uint8_t>(busId),
1721 static_cast<uint8_t>(targetAddr), writeData))
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301722 {
Matt Simmering68d9d402023-11-09 14:22:11 -08001723 log<level::ERR>("Controller write read request blocked!",
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301724 entry("BUS=%d", static_cast<uint8_t>(busId)),
Matt Simmering68d9d402023-11-09 14:22:11 -08001725 entry("ADDR=0x%x", static_cast<uint8_t>(targetAddr)));
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301726 return ipmi::responseInvalidFieldRequest();
1727 }
Yong Libd0503a2019-08-22 17:17:17 +08001728#endif // ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301729 std::vector<uint8_t> readBuf(readCount);
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001730 std::string i2cBus = "/dev/i2c-" +
1731 std::to_string(static_cast<uint8_t>(busId));
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301732
Matt Simmering68d9d402023-11-09 14:22:11 -08001733 ipmi::Cc ret = ipmi::i2cWriteRead(i2cBus, static_cast<uint8_t>(targetAddr),
Yong Li7dc4ac02019-08-23 17:44:32 +08001734 writeData, readBuf);
1735 if (ret != ipmi::ccSuccess)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301736 {
Yong Li7dc4ac02019-08-23 17:44:32 +08001737 return ipmi::response(ret);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301738 }
1739 return ipmi::responseSuccess(readBuf);
1740}
1741
Chris Austen6caf28b2015-10-13 12:40:40 -05001742void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +05301743{
Vernon Mauery86a50822019-03-25 13:11:36 -07001744 // <Get Device ID>
1745 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1746 ipmi::app::cmdGetDeviceId, ipmi::Privilege::User,
1747 ipmiAppGetDeviceId);
1748
Tom05732372016-09-06 17:21:23 +05301749 // <Get BT Interface Capabilities>
Vernon Mauerycc99ba42019-03-25 13:40:11 -07001750 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1751 ipmi::app::cmdGetBtIfaceCapabilities,
1752 ipmi::Privilege::User, ipmiAppGetBtCapabilities);
Chris Austen6caf28b2015-10-13 12:40:40 -05001753
Tom05732372016-09-06 17:21:23 +05301754 // <Reset Watchdog Timer>
Vernon Mauery11df4f62019-03-25 14:17:54 -07001755 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1756 ipmi::app::cmdResetWatchdogTimer,
1757 ipmi::Privilege::Operator, ipmiAppResetWatchdogTimer);
Chris Austen6caf28b2015-10-13 12:40:40 -05001758
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001759 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301760 ipmi::app::cmdGetSessionInfo, ipmi::Privilege::User,
1761 ipmiAppGetSessionInfo);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001762
Tom05732372016-09-06 17:21:23 +05301763 // <Set Watchdog Timer>
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +00001764 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1765 ipmi::app::cmdSetWatchdogTimer,
1766 ipmi::Privilege::Operator, ipmiSetWatchdogTimer);
Chris Austen6caf28b2015-10-13 12:40:40 -05001767
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +00001768 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1769 ipmi::app::cmdCloseSession, ipmi::Privilege::Callback,
1770 ipmiAppCloseSession);
1771
William A. Kennington III73f44512018-02-09 15:28:46 -08001772 // <Get Watchdog Timer>
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +00001773 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301774 ipmi::app::cmdGetWatchdogTimer, ipmi::Privilege::User,
1775 ipmiGetWatchdogTimer);
William A. Kennington III73f44512018-02-09 15:28:46 -08001776
Tom05732372016-09-06 17:21:23 +05301777 // <Get Self Test Results>
Vernon Maueryb84a5282019-03-25 13:39:03 -07001778 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1779 ipmi::app::cmdGetSelfTestResults,
1780 ipmi::Privilege::User, ipmiAppGetSelfTestResults);
Nan Li41fa24a2016-11-10 20:12:37 +08001781
Tom05732372016-09-06 17:21:23 +05301782 // <Get Device GUID>
Vernon Mauery15541322019-03-25 13:33:03 -07001783 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1784 ipmi::app::cmdGetDeviceGuid, ipmi::Privilege::User,
1785 ipmiAppGetDeviceGuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -05001786
Tom05732372016-09-06 17:21:23 +05301787 // <Set ACPI Power State>
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +00001788 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1789 ipmi::app::cmdSetAcpiPowerState,
1790 ipmi::Privilege::Admin, ipmiSetAcpiPowerState);
Yong Li18d77262018-10-09 01:59:45 +08001791 // <Get ACPI Power State>
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +00001792 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1793 ipmi::app::cmdGetAcpiPowerState,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301794 ipmi::Privilege::User, ipmiGetAcpiPowerState);
Yong Li18d77262018-10-09 01:59:45 +08001795
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301796 // Note: For security reason, this command will be registered only when
Matt Simmering68d9d402023-11-09 14:22:11 -08001797 // there are proper I2C Controller write read allowlist
1798 if (populateI2CControllerWRAllowlist())
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301799 {
Matt Simmering68d9d402023-11-09 14:22:11 -08001800 // Note: For security reasons, registering controller write read as
1801 // admin privilege command, even though IPMI 2.0 specification allows it
1802 // as operator privilege.
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301803 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1804 ipmi::app::cmdMasterWriteRead,
Matt Simmering68d9d402023-11-09 14:22:11 -08001805 ipmi::Privilege::Admin, ipmiControllerWriteRead);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301806 }
1807
Marri Devender Rao5e007a52018-01-08 06:18:36 -06001808 // <Get System GUID Command>
Vernon Maueryb90a5322019-03-25 13:36:55 -07001809 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1810 ipmi::app::cmdGetSystemGuid, ipmi::Privilege::User,
1811 ipmiAppGetSystemGuid);
Tom Joseph7cbe2282018-03-21 21:17:33 +05301812
1813 // <Get Channel Cipher Suites Command>
Ayushi Smriti5c3b72c2019-08-30 13:47:31 +00001814 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1815 ipmi::app::cmdGetChannelCipherSuites,
Vernon Mauery79b4eea2019-11-07 09:51:39 -08001816 ipmi::Privilege::None, getChannelCipherSuites);
Vernon Maueryd2a57de2019-03-25 12:45:28 -07001817
Xo Wangf542e8b2017-08-09 15:34:16 -07001818 // <Get System Info Command>
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001819 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1820 ipmi::app::cmdGetSystemInfoParameters,
1821 ipmi::Privilege::User, ipmiAppGetSystemInfo);
Jia, chunhui449f2162019-09-11 16:51:51 +08001822 // <Set System Info Command>
1823 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1824 ipmi::app::cmdSetSystemInfoParameters,
1825 ipmi::Privilege::Admin, ipmiAppSetSystemInfo);
vishwabmcba0bd5f2015-09-30 16:50:23 +05301826 return;
1827}