xref: /openbmc/phosphor-host-ipmid/apphandler.cpp (revision a6c307a84fe43679ece859789666cca4c391cd38)
1 #include "config.h"
2 
3 #include <arpa/inet.h>
4 #include <fcntl.h>
5 #include <limits.h>
6 #include <linux/i2c-dev.h>
7 #include <linux/i2c.h>
8 #include <sys/ioctl.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <systemd/sd-bus.h>
12 #include <unistd.h>
13 
14 #include <app/channel.hpp>
15 #include <app/watchdog.hpp>
16 #include <apphandler.hpp>
17 #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>
24 #include <phosphor-logging/lg2.hpp>
25 #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>
34 #include <array>
35 #include <charconv>
36 #include <cstddef>
37 #include <cstdint>
38 #include <filesystem>
39 #include <fstream>
40 #include <memory>
41 #include <regex>
42 #include <string>
43 #include <string_view>
44 #include <tuple>
45 #include <vector>
46 
47 extern sd_bus* bus;
48 
49 constexpr auto bmc_state_interface = "xyz.openbmc_project.State.BMC";
50 constexpr auto bmc_state_property = "CurrentBMCState";
51 constexpr auto versionPurposeHostEnd = ".Host";
52 
53 static constexpr auto redundancyIntf =
54     "xyz.openbmc_project.Software.RedundancyPriority";
55 static constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
56 static constexpr auto activationIntf =
57     "xyz.openbmc_project.Software.Activation";
58 static constexpr auto softwareRoot = "/xyz/openbmc_project/software";
59 
60 void registerNetFnAppFunctions() __attribute__((constructor));
61 
62 using namespace phosphor::logging;
63 using namespace sdbusplus::error::xyz::openbmc_project::common;
64 using Version = sdbusplus::server::xyz::openbmc_project::software::Version;
65 using Activation =
66     sdbusplus::server::xyz::openbmc_project::software::Activation;
67 using BMC = sdbusplus::server::xyz::openbmc_project::state::BMC;
68 namespace fs = std::filesystem;
69 
70 #ifdef ENABLE_I2C_WHITELIST_CHECK
71 typedef struct
72 {
73     uint8_t busId;
74     uint8_t targetAddr;
75     uint8_t targetAddrMask;
76     std::vector<uint8_t> data;
77     std::vector<uint8_t> dataMask;
78 } i2cControllerWRAllowlist;
79 
getWRAllowlist()80 static std::vector<i2cControllerWRAllowlist>& getWRAllowlist()
81 {
82     static std::vector<i2cControllerWRAllowlist> wrAllowlist;
83     return wrAllowlist;
84 }
85 
86 static constexpr const char* i2cControllerWRAllowlistFile =
87     "/usr/share/ipmi-providers/master_write_read_white_list.json";
88 
89 static constexpr const char* filtersStr = "filters";
90 static constexpr const char* busIdStr = "busId";
91 static constexpr const char* targetAddrStr = "slaveAddr";
92 static constexpr const char* targetAddrMaskStr = "slaveAddrMask";
93 static constexpr const char* cmdStr = "command";
94 static constexpr const char* cmdMaskStr = "commandMask";
95 static constexpr int base_16 = 16;
96 #endif // ENABLE_I2C_WHITELIST_CHECK
97 static constexpr uint8_t oemCmdStart = 192;
98 static constexpr uint8_t invalidParamSelectorStart = 8;
99 static constexpr uint8_t invalidParamSelectorEnd = 191;
100 
101 /**
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  */
getActiveSoftwareVersionInfo(ipmi::Context::ptr ctx)112 std::string getActiveSoftwareVersionInfo(ipmi::Context::ptr ctx)
113 {
114     std::string revision{};
115     ipmi::ObjectTree objectTree;
116     try
117     {
118         objectTree =
119             ipmi::getAllDbusObjects(*ctx->bus, softwareRoot, redundancyIntf);
120     }
121     catch (const sdbusplus::exception_t& e)
122     {
123         lg2::error("Failed to fetch redundancy object from dbus, "
124                    "interface: {INTERFACE},  error: {ERROR}",
125                    "INTERFACE", redundancyIntf, "ERROR", e);
126         elog<InternalFailure>();
127     }
128 
129     auto objectFound = false;
130     for (auto& softObject : objectTree)
131     {
132         auto service =
133             ipmi::getService(*ctx->bus, redundancyIntf, softObject.first);
134         auto objValueTree =
135             ipmi::getManagedObjects(*ctx->bus, service, softwareRoot);
136 
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);
146                 auto priority =
147                     std::get<uint8_t>(redundancyPriorityProps.at("Priority"));
148                 auto purpose =
149                     std::get<std::string>(versionProps.at("Purpose"));
150                 auto activation =
151                     std::get<std::string>(activationProps.at("Activation"));
152                 auto version =
153                     std::get<std::string>(versionProps.at("Version"));
154                 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                 lg2::error("error message: {ERROR}", "ERROR", e);
170             }
171         }
172     }
173 
174     if (!objectFound)
175     {
176         lg2::error("Could not found an BMC software Object");
177         elog<InternalFailure>();
178     }
179 
180     return revision;
181 }
182 
getCurrentBmcState()183 bool getCurrentBmcState()
184 {
185     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
186 
187     // Get the Inventory object implementing the BMC interface
188     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);
193 
194     return std::holds_alternative<std::string>(variant) &&
195            BMC::convertBMCStateFromString(std::get<std::string>(variant)) ==
196                BMC::BMCState::Ready;
197 }
198 
getCurrentBmcStateWithFallback(const bool fallbackAvailability)199 bool 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 
213 namespace acpi_state
214 {
215 using namespace sdbusplus::server::xyz::openbmc_project::control::power;
216 
217 const static constexpr char* acpiInterface =
218     "xyz.openbmc_project.Control.Power.ACPIPowerState";
219 const static constexpr char* sysACPIProp = "SysACPIStatus";
220 const static constexpr char* devACPIProp = "DevACPIStatus";
221 
222 enum class PowerStateType : uint8_t
223 {
224     sysPowerState = 0x00,
225     devPowerState = 0x01,
226 };
227 
228 // Defined in 20.6 of ipmi doc
229 enum 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 
248 static constexpr uint8_t stateChanged = 0x80;
249 
250 std::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 
isValidACPIState(acpi_state::PowerStateType type,uint8_t state)266 bool 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 
305 /** @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  **/
ipmiSetAcpiPowerState(uint8_t sysAcpiState,uint8_t devAcpiState)311 ipmi::RspType<> ipmiSetAcpiPowerState(uint8_t sysAcpiState,
312                                       uint8_t devAcpiState)
313 {
314     auto s = static_cast<uint8_t>(acpi_state::PowerState::unknown);
315 
316     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
317 
318     auto value = acpi_state::ACPIPowerState::ACPI::Unknown;
319 
320     if (sysAcpiState & acpi_state::stateChanged)
321     {
322         // set system power state
323         s = sysAcpiState & ~acpi_state::stateChanged;
324 
325         if (!acpi_state::isValidACPIState(
326                 acpi_state::PowerStateType::sysPowerState, s))
327         {
328             lg2::error("set_acpi_power sys invalid input, S: {S}", "S", s);
329             return ipmi::responseParmOutOfRange();
330         }
331 
332         // valid input
333         if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
334         {
335             lg2::debug("No change for system power state");
336         }
337         else
338         {
339             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                 });
344 
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             {
358                 lg2::error("Failed in set ACPI system property: {ERROR}",
359                            "ERROR", e);
360                 return ipmi::responseUnspecifiedError();
361             }
362         }
363     }
364     else
365     {
366         lg2::debug("Do not change system power state");
367     }
368 
369     if (devAcpiState & acpi_state::stateChanged)
370     {
371         // set device power state
372         s = devAcpiState & ~acpi_state::stateChanged;
373         if (!acpi_state::isValidACPIState(
374                 acpi_state::PowerStateType::devPowerState, s))
375         {
376             lg2::error("set_acpi_power dev invalid input, S: {S}", "S", s);
377             return ipmi::responseParmOutOfRange();
378         }
379 
380         // valid input
381         if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
382         {
383             lg2::debug("No change for device power state");
384         }
385         else
386         {
387             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                 });
392 
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             {
406                 lg2::error("Failed in set ACPI device property: {ERROR}",
407                            "ERROR", e);
408                 return ipmi::responseUnspecifiedError();
409             }
410         }
411     }
412     else
413     {
414         lg2::debug("Do not change device power state");
415     }
416     return ipmi::responseSuccess();
417 }
418 
419 /**
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  **/
426 ipmi::RspType<uint8_t, // acpiSystemPowerState
427               uint8_t  // acpiDevicePowerState
428               >
ipmiGetAcpiPowerState()429     ipmiGetAcpiPowerState()
430 {
431     uint8_t sysAcpiState;
432     uint8_t devAcpiState;
433 
434     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
435 
436     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(
444             std::get<std::string>(sysACPIVal));
445         sysAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(sysACPI));
446 
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(
451             std::get<std::string>(devACPIVal));
452         devAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(devACPI));
453     }
454     catch (const InternalFailure& e)
455     {
456         return ipmi::responseUnspecifiedError();
457     }
458 
459     return ipmi::responseSuccess(sysAcpiState, devAcpiState);
460 }
461 
462 typedef struct
463 {
464     char major;
465     char minor;
466     uint8_t aux[4];
467 } Revision;
468 
469 /* 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 /*                                                                          */
477 /* 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                   */
convertVersion(std::string s,Revision & rev)504 int convertVersion(std::string s, Revision& rev)
505 {
506     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};
509     std::regex fw_regex(FW_VER_REGEX);
510     std::smatch m;
511     Revision r = {0};
512     size_t val;
513 
514     if (std::regex_search(s, m, fw_regex))
515     {
516         if (m.size() < *std::max_element(matches.begin(), matches.end()))
517         { // max index higher than match count
518             return -1;
519         }
520 
521         // convert major
522         {
523             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())
527             { // failed to convert major string
528                 return -1;
529             }
530 
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             }
542         }
543 
544         // convert minor
545         {
546             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())
550             { // failed to convert minor string
551                 return -1;
552             }
553             r.minor = val & 0xFF;
554         }
555 
556         // 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 
566                 std::string str = m[matches[i + 2]].str();
567                 const char* cstr = str.c_str();
568                 auto [ptr,
569                       ec] = std::from_chars(cstr, cstr + str.size(), val, 16);
570                 if (ec != std::errc() || ptr != cstr + str.size())
571                 { // 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 
584         // all matched
585         rev = r;
586         return 0;
587     }
588 
589     return -1;
590 }
591 
592 /* @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  */
606 ipmi::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               >
ipmiAppGetDeviceId(ipmi::Context::ptr ctx)616     ipmiAppGetDeviceId([[maybe_unused]] ipmi::Context::ptr ctx)
617 {
618     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;
629     static bool dev_id_initialized = false;
630     static bool defaultActivationSetting = true;
631     const char* filename = "/usr/share/ipmi-providers/dev_id.json";
632     constexpr auto ipmiDevIdStateShift = 7;
633     constexpr auto ipmiDevIdFw1Mask = ~(1 << ipmiDevIdStateShift);
634 
635 #ifdef GET_DBUS_ACTIVE_SOFTWARE
636     static bool haveBMCVersion = false;
637     if (!haveBMCVersion || !dev_id_initialized)
638     {
639         int r = -1;
640         Revision rev = {0, 0, {0, 0, 0, 0}};
641         try
642         {
643             auto version = getActiveSoftwareVersionInfo(ctx);
644             r = convertVersion(version, rev);
645         }
646         catch (const std::exception& e)
647         {
648             lg2::error("error message: {ERROR}", "ERROR", e);
649         }
650 
651         if (r >= 0)
652         {
653             // bit7 identifies if the device is available
654             // 0=normal operation
655             // 1=device firmware, SDR update,
656             // or self-initialization in progress.
657             // The availability may change in run time, so mask here
658             // and initialize later.
659             devId.fw[0] = rev.major & ipmiDevIdFw1Mask;
660 
661             rev.minor = (rev.minor > 99 ? 99 : rev.minor);
662             devId.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16;
663             std::memcpy(&devId.aux, rev.aux, sizeof(rev.aux));
664             haveBMCVersion = true;
665         }
666     }
667 #endif
668     if (!dev_id_initialized)
669     {
670         // IPMI Spec version 2.0
671         devId.ipmiVer = 2;
672 
673         std::ifstream devIdFile(filename);
674         if (devIdFile.is_open())
675         {
676             auto data = nlohmann::json::parse(devIdFile, nullptr, false);
677             if (!data.is_discarded())
678             {
679                 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);
684 #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                 }
691 
692                 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 
705                 // Set the availablitity of the BMC.
706                 defaultActivationSetting = data.value("availability", true);
707 
708                 // Don't read the file every time if successful
709                 dev_id_initialized = true;
710             }
711             else
712             {
713                 lg2::error("Device ID JSON parser failure");
714                 return ipmi::responseUnspecifiedError();
715             }
716         }
717         else
718         {
719             lg2::error("Device ID file not found");
720             return ipmi::responseUnspecifiedError();
721         }
722     }
723 
724     // Set availability to the actual current BMC state
725     devId.fw[0] &= ipmiDevIdFw1Mask;
726     if (!getCurrentBmcStateWithFallback(defaultActivationSetting))
727     {
728         devId.fw[0] |= (1 << ipmiDevIdStateShift);
729     }
730 
731     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);
734 }
735 
ipmiAppGetSelfTestResults()736 auto ipmiAppGetSelfTestResults() -> ipmi::RspType<uint8_t, uint8_t>
737 {
738     // Byte 2:
739     //  55h - No error.
740     //  56h - Self Test function not implemented in this controller.
741     //  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.
758     constexpr uint8_t notImplemented = 0x56;
759     constexpr uint8_t zero = 0;
760     return ipmi::responseSuccess(notImplemented, zero);
761 }
762 
763 static constexpr size_t uuidBinaryLength = 16;
rfc4122ToIpmi(std::string rfc4122)764 static std::array<uint8_t, uuidBinaryLength> rfc4122ToIpmi(std::string rfc4122)
765 {
766     using Argument = xyz::openbmc_project::common::InvalidArgument;
767     // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
768     // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte
769     // order
770     // Ex: 0x2332fc2c40e66298e511f2782395a361
771     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)
775     {
776         rfc4122.erase(std::remove(rfc4122.begin(), rfc4122.end(), '-'),
777                       rfc4122.end());
778     }
779     if (rfc4122.size() != uuidHexLength)
780     {
781         elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
782                               Argument::ARGUMENT_VALUE(rfc4122.c_str()));
783     }
784     for (size_t ind = 0; ind < uuidHexLength; ind += 2)
785     {
786         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
793         {
794             b = std::stoul(v, &err, 16);
795         }
796         catch (const std::exception& e)
797         {
798             elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
799                                   Argument::ARGUMENT_VALUE(rfc4122.c_str()));
800         }
801         // 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);
808     }
809     return uuid;
810 }
811 
ipmiAppGetDeviceGuid()812 auto 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
817 
818     // 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);
822 
823     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);
826 
827     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);
832 }
833 
ipmiAppGetBtCapabilities()834 auto ipmiAppGetBtCapabilities()
835     -> ipmi::RspType<uint8_t, uint8_t, uint8_t, uint8_t, uint8_t>
836 {
837     // 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.
839     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;
844 
845     return ipmi::responseSuccess(nrOutstanding, inputBufferSize,
846                                  outputBufferSize, transactionTime, nrRetries);
847 }
848 
ipmiAppGetSystemGuid(ipmi::Context::ptr & ctx)849 auto ipmiAppGetSystemGuid(ipmi::Context::ptr& ctx)
850     -> ipmi::RspType<std::array<uint8_t, 16>>
851 {
852     static constexpr auto uuidInterface = "xyz.openbmc_project.Common.UUID";
853     static constexpr auto uuidProperty = "UUID";
854 
855     // Get the Inventory object implementing BMC interface
856     ipmi::DbusObjectInfo objectInfo{};
857     boost::system::error_code ec = ipmi::getDbusObject(
858         ctx, uuidInterface, ipmi::sensor::inventoryRoot, objectInfo);
859     if (ec.value())
860     {
861         lg2::error("Failed to locate System UUID object, "
862                    "interface: {INTERFACE}, error: {ERROR}",
863                    "INTERFACE", uuidInterface, "ERROR", ec.message());
864     }
865 
866     // Read UUID property value from bmcObject
867     // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
868     std::string rfc4122Uuid{};
869     ec = ipmi::getDbusProperty(ctx, objectInfo.second, objectInfo.first,
870                                uuidInterface, uuidProperty, rfc4122Uuid);
871     if (ec.value())
872     {
873         lg2::error("Failed to read System UUID property, "
874                    "interface: {INTERFACE}, property: {PROPERTY}, "
875                    "error: {ERROR}",
876                    "INTERFACE", uuidInterface, "PROPERTY", uuidProperty,
877                    "ERROR", ec.message());
878         return ipmi::responseUnspecifiedError();
879     }
880     std::array<uint8_t, 16> uuid;
881     try
882     {
883         // convert to IPMI format
884         uuid = rfc4122ToIpmi(rfc4122Uuid);
885     }
886     catch (const InvalidArgument& e)
887     {
888         lg2::error("Failed in parsing BMC UUID property, "
889                    "interface: {INTERFACE}, property: {PROPERTY}, "
890                    "value: {VALUE}, error: {ERROR}",
891                    "INTERFACE", uuidInterface, "PROPERTY", uuidProperty,
892                    "VALUE", rfc4122Uuid, "ERROR", e);
893         return ipmi::responseUnspecifiedError();
894     }
895     return ipmi::responseSuccess(uuid);
896 }
897 
898 /**
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  **/
setSessionState(std::shared_ptr<sdbusplus::asio::connection> & busp,const std::string & service,const std::string & obj)911 uint8_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     }
927     catch (const std::exception& e)
928     {
929         lg2::error("Failed in getting session state property, "
930                    "service: {SERVICE}, object path: {OBJECT_PATH}, "
931                    "interface: {INTERFACE}, error: {ERROR}",
932                    "SERVICE", service, "OBJECT_PATH", obj, "INTERFACE",
933                    session::sessionIntf, "ERROR", e);
934         return ipmi::ccUnspecifiedError;
935     }
936 
937     return ipmi::ccInvalidFieldRequest;
938 }
939 
ipmiAppCloseSession(uint32_t reqSessionId,std::optional<uint8_t> requestSessionHandle)940 ipmi::RspType<> ipmiAppCloseSession(uint32_t reqSessionId,
941                                     std::optional<uint8_t> requestSessionHandle)
942 {
943     auto busp = getSdBus();
944     uint8_t reqSessionHandle =
945         requestSessionHandle.value_or(session::defaultSessionHandle);
946 
947     if (reqSessionId == session::sessionZero &&
948         reqSessionHandle == session::defaultSessionHandle)
949     {
950         return ipmi::response(session::ccInvalidSessionId);
951     }
952 
953     if (reqSessionId == session::sessionZero &&
954         reqSessionHandle == session::invalidSessionHandle)
955     {
956         return ipmi::response(session::ccInvalidSessionHandle);
957     }
958 
959     if (reqSessionId != session::sessionZero &&
960         reqSessionHandle != session::defaultSessionHandle)
961     {
962         return ipmi::response(ipmi::ccInvalidFieldRequest);
963     }
964 
965     try
966     {
967         ipmi::ObjectTree objectTree = ipmi::getAllDbusObjects(
968             *busp, session::sessionManagerRootPath, session::sessionIntf);
969 
970         for (auto& objectTreeItr : objectTree)
971         {
972             const std::string obj = objectTreeItr.first;
973 
974             if (isSessionObjectMatched(obj, reqSessionId, reqSessionHandle))
975             {
976                 auto& serviceMap = objectTreeItr.second;
977 
978                 // Session id and session handle are unique for each session.
979                 // Session id and handler are retrived from the object path and
980                 // object path will be unique for each session. Checking if
981                 // multiple objects exist with same object path under multiple
982                 // services.
983                 if (serviceMap.size() != 1)
984                 {
985                     return ipmi::responseUnspecifiedError();
986                 }
987 
988                 auto itr = serviceMap.begin();
989                 const std::string service = itr->first;
990                 return ipmi::response(setSessionState(busp, service, obj));
991             }
992         }
993     }
994     catch (const sdbusplus::exception_t& e)
995     {
996         lg2::error("Failed to fetch object from dbus, "
997                    "interface: {INTERFACE}, error: {ERROR}",
998                    "INTERFACE", session::sessionIntf, "ERROR", e);
999         return ipmi::responseUnspecifiedError();
1000     }
1001 
1002     return ipmi::responseInvalidFieldRequest();
1003 }
1004 
getTotalSessionCount()1005 uint8_t getTotalSessionCount()
1006 {
1007     uint8_t count = 0, ch = 0;
1008 
1009     while (ch < ipmi::maxIpmiChannels &&
1010            count < session::maxNetworkInstanceSupported)
1011     {
1012         ipmi::ChannelInfo chInfo{};
1013         ipmi::getChannelInfo(ch, chInfo);
1014         if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) ==
1015             ipmi::EChannelMediumType::lan8032)
1016         {
1017             count++;
1018         }
1019         ch++;
1020     }
1021     return count * session::maxSessionCountPerChannel;
1022 }
1023 
1024 /**
1025  * @brief get session info request data.
1026  *
1027  * This function validates the request data and retrive request session id,
1028  * session handle.
1029  *
1030  * @param[in] ctx - context of current session.
1031  * @param[in] sessionIndex - request session index
1032  * @param[in] payload - input payload
1033  * @param[in] reqSessionId - unpacked session Id will be asigned
1034  * @param[in] reqSessionHandle - unpacked session handle will be asigned
1035  *
1036  * @return success completion code if request data is valid
1037  * else return the correcponding error completion code.
1038  **/
getSessionInfoRequestData(const ipmi::Context::ptr ctx,const uint8_t sessionIndex,ipmi::message::Payload & payload,uint32_t & reqSessionId,uint8_t & reqSessionHandle)1039 uint8_t getSessionInfoRequestData(
1040     const ipmi::Context::ptr ctx, const uint8_t sessionIndex,
1041     ipmi::message::Payload& payload, uint32_t& reqSessionId,
1042     uint8_t& reqSessionHandle)
1043 {
1044     if ((sessionIndex > session::maxSessionCountPerChannel) &&
1045         (sessionIndex < session::searchSessionByHandle))
1046     {
1047         return ipmi::ccInvalidFieldRequest;
1048     }
1049 
1050     switch (sessionIndex)
1051     {
1052         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 
1075         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 
getSessionState(ipmi::Context::ptr ctx,const std::string & service,const std::string & objPath,uint8_t & sessionState)1114 uint8_t getSessionState(ipmi::Context::ptr ctx, const std::string& service,
1115                         const std::string& objPath, uint8_t& sessionState)
1116 {
1117     boost::system::error_code ec = ipmi::getDbusProperty(
1118         ctx, service, objPath, session::sessionIntf, "State", sessionState);
1119     if (ec)
1120     {
1121         lg2::error("Failed to fetch state property, service: {SERVICE}, "
1122                    "object path: {OBJECTPATH}, interface: {INTERFACE}, "
1123                    "error: {ERROR}",
1124                    "SERVICE", service, "OBJECTPATH", objPath, "INTERFACE",
1125                    session::sessionIntf, "ERROR", ec.message());
1126         return ipmi::ccUnspecifiedError;
1127     }
1128     return ipmi::ccSuccess;
1129 }
1130 
1131 static constexpr uint8_t macAddrLen = 6;
1132 /** 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  */
1145 using 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>;
1148 
1149 /** @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  */
getSessionDetails(ipmi::Context::ptr ctx,const std::string & service,const std::string & objPath,uint8_t & sessionHandle,uint8_t & sessionState,SessionDetails & details)1160 ipmi::Cc getSessionDetails(ipmi::Context::ptr ctx, const std::string& service,
1161                            const std::string& objPath, uint8_t& sessionHandle,
1162                            uint8_t& sessionState, SessionDetails& details)
1163 {
1164     ipmi::PropertyMap sessionProps;
1165     boost::system::error_code ec = ipmi::getAllDbusProperties(
1166         ctx, service, objPath, session::sessionIntf, sessionProps);
1167 
1168     if (ec)
1169     {
1170         lg2::error("Failed to fetch state property, service: {SERVICE}, "
1171                    "object path: {OBJECTPATH}, interface: {INTERFACE}, "
1172                    "error: {ERROR}",
1173                    "SERVICE", service, "OBJECTPATH", objPath, "INTERFACE",
1174                    session::sessionIntf, "ERROR", ec.message());
1175         return ipmi::ccUnspecifiedError;
1176     }
1177 
1178     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     {
1182         sessionHandle =
1183             ipmi::mappedVariant<uint8_t>(sessionProps, "SessionHandle", 0);
1184         std::get<0>(details) =
1185             ipmi::mappedVariant<uint8_t>(sessionProps, "UserID", 0xff);
1186         // 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)
1190         std::get<4>(details) =
1191             ipmi::mappedVariant<uint8_t>(sessionProps, "ChannelNum", 0xff);
1192         constexpr uint4_t rmcpPlusProtocol = 1;
1193         std::get<5>(details) = rmcpPlusProtocol;
1194         std::get<6>(details) =
1195             ipmi::mappedVariant<uint32_t>(sessionProps, "RemoteIPAddr", 0);
1196         // std::get<7>(details) = {{0}}; // default constructed to all 0
1197         std::get<8>(details) =
1198             ipmi::mappedVariant<uint16_t>(sessionProps, "RemotePort", 0);
1199     }
1200 
1201     return ipmi::ccSuccess;
1202 }
1203 
1204 ipmi::RspType<uint8_t, // session handle,
1205               uint8_t, // total session count
1206               uint8_t, // active session count
1207               std::optional<SessionDetails>>
ipmiAppGetSessionInfo(ipmi::Context::ptr ctx,uint8_t sessionIndex,ipmi::message::Payload & payload)1208     ipmiAppGetSessionInfo(ipmi::Context::ptr ctx, uint8_t sessionIndex,
1209                           ipmi::message::Payload& payload)
1210 {
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(
1217         ctx, sessionIndex, payload, reqSessionId, reqSessionHandle);
1218 
1219     if (completionCode)
1220     {
1221         return ipmi::response(completionCode);
1222     }
1223     ipmi::ObjectTree objectTree;
1224     boost::system::error_code ec = ipmi::getAllDbusObjects(
1225         ctx, session::sessionManagerRootPath, session::sessionIntf, objectTree);
1226     if (ec)
1227     {
1228         lg2::error("Failed to fetch object from dbus, "
1229                    "interface: {INTERFACE}, error: {ERROR}",
1230                    "INTERFACE", session::sessionIntf, "ERROR", ec.message());
1231         return ipmi::responseUnspecifiedError();
1232     }
1233 
1234     uint8_t totalSessionCount = getTotalSessionCount();
1235     uint8_t activeSessionCount = 0;
1236     uint8_t sessionHandle = session::defaultSessionHandle;
1237     uint8_t activeSessionHandle = 0;
1238     std::optional<SessionDetails> maybeDetails;
1239     uint8_t index = 0;
1240     for (auto& objectTreeItr : objectTree)
1241     {
1242         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;
1261         completionCode =
1262             getSessionState(ctx, service, objectPath, sessionState);
1263         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 
1273         if (index == sessionIndex || reqSessionId == sessionId ||
1274             reqSessionHandle == sessionHandle)
1275         {
1276             SessionDetails details{};
1277             completionCode = getSessionDetails(ctx, service, objectPath,
1278                                                sessionHandle, state, details);
1279 
1280             if (completionCode)
1281             {
1282                 return ipmi::response(completionCode);
1283             }
1284             activeSessionHandle = sessionHandle;
1285             maybeDetails = std::move(details);
1286         }
1287     }
1288 
1289     if (state == static_cast<uint8_t>(session::State::active) ||
1290         state == static_cast<uint8_t>(session::State::tearDownInProgress))
1291     {
1292         return ipmi::responseSuccess(activeSessionHandle, totalSessionCount,
1293                                      activeSessionCount, maybeDetails);
1294     }
1295 
1296     return ipmi::responseInvalidFieldRequest();
1297 }
1298 
getSysFWVersion(ipmi::Context::ptr & ctx)1299 std::optional<std::string> getSysFWVersion(ipmi::Context::ptr& ctx)
1300 {
1301     /*
1302      * The System Firmware version is detected via following steps:
1303      * - Get all of object paths that include
1304      * "xyz.openbmc_project.Software.Version" interface.
1305      * - Get the Purpose property of above object paths.
1306      * - If the Purpose is Host then get the Version property.
1307      */
1308     ipmi::ObjectTree objectTree;
1309     boost::system::error_code ec =
1310         ipmi::getAllDbusObjects(ctx, softwareRoot, versionIntf, objectTree);
1311     if (ec.value())
1312     {
1313         return std::nullopt;
1314     }
1315 
1316     for (const auto& [objPath, serviceMap] : objectTree)
1317     {
1318         for (const auto& [service, intfs] : serviceMap)
1319         {
1320             ipmi::PropertyMap props;
1321             ec = ipmi::getAllDbusProperties(ctx, service, objPath, versionIntf,
1322                                             props);
1323             if (ec.value())
1324             {
1325                 continue;
1326             }
1327 
1328             std::string purposeProp = std::string(
1329                 ipmi::mappedVariant<std::string>(props, "Purpose", ""));
1330 
1331             if (!purposeProp.ends_with(versionPurposeHostEnd))
1332             {
1333                 continue;
1334             }
1335 
1336             std::string sysFWVersion = std::string(
1337                 ipmi::mappedVariant<std::string>(props, "Version", ""));
1338 
1339             if (sysFWVersion.empty())
1340             {
1341                 return std::nullopt;
1342             }
1343 
1344             return sysFWVersion;
1345         }
1346     }
1347 
1348     return std::nullopt;
1349 }
1350 
1351 static std::unique_ptr<SysInfoParamStore> sysInfoParamStore;
1352 
sysInfoReadSystemName()1353 static std::string sysInfoReadSystemName()
1354 {
1355     // Use the BMC hostname as the "System Name."
1356     char hostname[HOST_NAME_MAX + 1] = {};
1357     if (gethostname(hostname, HOST_NAME_MAX) != 0)
1358     {
1359         perror("System info parameter: system name");
1360     }
1361     return hostname;
1362 }
1363 
1364 static constexpr uint8_t paramRevision = 0x11;
1365 static constexpr size_t configParameterLength = 16;
1366 
1367 static constexpr size_t smallChunkSize = 14;
1368 static constexpr size_t fullChunkSize = 16;
1369 static constexpr uint8_t progressMask = 0x3;
1370 static constexpr uint8_t maxValidEncodingData = 0x02;
1371 
1372 static constexpr uint8_t setComplete = 0x0;
1373 static constexpr uint8_t setInProgress = 0x1;
1374 static uint8_t transferStatus = setComplete;
1375 
1376 static constexpr uint8_t configDataOverhead = 2;
1377 
1378 namespace ipmi
1379 {
1380 constexpr Cc ccParmNotSupported = 0x80;
1381 constexpr Cc ccSetInProgressActive = 0x81;
1382 
responseParmNotSupported()1383 static inline auto responseParmNotSupported()
1384 {
1385     return response(ccParmNotSupported);
1386 }
responseSetInProgressActive()1387 static inline auto responseSetInProgressActive()
1388 {
1389     return response(ccSetInProgressActive);
1390 }
1391 } // namespace ipmi
1392 
1393 ipmi::RspType<uint8_t,                // Parameter revision
1394               std::optional<uint8_t>, // data1 / setSelector / ProgressStatus
1395               std::optional<std::vector<uint8_t>>> // data2-17
ipmiAppGetSystemInfo(ipmi::Context::ptr ctx,uint7_t reserved,bool getRevision,uint8_t paramSelector,uint8_t setSelector,uint8_t BlockSelector)1396     ipmiAppGetSystemInfo(ipmi::Context::ptr ctx, uint7_t reserved,
1397                          bool getRevision, uint8_t paramSelector,
1398                          uint8_t setSelector, uint8_t BlockSelector)
1399 {
1400     if (reserved || (paramSelector >= invalidParamSelectorStart &&
1401                      paramSelector <= invalidParamSelectorEnd))
1402     {
1403         return ipmi::responseInvalidFieldRequest();
1404     }
1405     if (paramSelector >= oemCmdStart)
1406     {
1407         return ipmi::responseParmNotSupported();
1408     }
1409     if (getRevision)
1410     {
1411         return ipmi::responseSuccess(paramRevision, std::nullopt, std::nullopt);
1412     }
1413 
1414     if (paramSelector == 0)
1415     {
1416         return ipmi::responseSuccess(paramRevision, transferStatus,
1417                                      std::nullopt);
1418     }
1419 
1420     if (BlockSelector != 0) // 00h if parameter does not require a block number
1421     {
1422         return ipmi::responseParmNotSupported();
1423     }
1424 
1425     if (sysInfoParamStore == nullptr)
1426     {
1427         sysInfoParamStore = std::make_unique<SysInfoParamStore>();
1428         sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1429                                   sysInfoReadSystemName);
1430     }
1431 
1432     if (paramSelector == IPMI_SYSINFO_SYSTEM_FW_VERSION)
1433     {
1434         auto fwVersion = getSysFWVersion(ctx);
1435 
1436         if (fwVersion == std::nullopt)
1437         {
1438             return ipmi::responseUnspecifiedError();
1439         }
1440         sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_FW_VERSION, *fwVersion);
1441     }
1442 
1443     // Parameters other than Set In Progress are assumed to be strings.
1444     std::tuple<bool, std::string> ret =
1445         sysInfoParamStore->lookup(paramSelector);
1446     bool found = std::get<0>(ret);
1447     if (!found)
1448     {
1449         return ipmi::responseSensorInvalid();
1450     }
1451     std::string& paramString = std::get<1>(ret);
1452     std::vector<uint8_t> configData;
1453     size_t count = 0;
1454     if (setSelector == 0)
1455     {                               // First chunk has only 14 bytes.
1456         configData.emplace_back(0); // encoding
1457         configData.emplace_back(paramString.length()); // string length
1458         count = std::min(paramString.length(), smallChunkSize);
1459         configData.resize(count + configDataOverhead);
1460         std::copy_n(paramString.begin(), count,
1461                     configData.begin() + configDataOverhead); // 14 bytes chunk
1462 
1463         // Append zero's to remaining bytes
1464         if (configData.size() < configParameterLength)
1465         {
1466             std::fill_n(std::back_inserter(configData),
1467                         configParameterLength - configData.size(), 0x00);
1468         }
1469     }
1470     else
1471     {
1472         size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
1473         if (offset >= paramString.length())
1474         {
1475             return ipmi::responseParmOutOfRange();
1476         }
1477         count = std::min(paramString.length() - offset, fullChunkSize);
1478         configData.resize(count);
1479         std::copy_n(paramString.begin() + offset, count,
1480                     configData.begin()); // 16 bytes chunk
1481     }
1482     return ipmi::responseSuccess(paramRevision, setSelector, configData);
1483 }
1484 
ipmiAppSetSystemInfo(uint8_t paramSelector,uint8_t data1,std::vector<uint8_t> configData)1485 ipmi::RspType<> ipmiAppSetSystemInfo(uint8_t paramSelector, uint8_t data1,
1486                                      std::vector<uint8_t> configData)
1487 {
1488     if (paramSelector >= invalidParamSelectorStart &&
1489         paramSelector <= invalidParamSelectorEnd)
1490     {
1491         return ipmi::responseInvalidFieldRequest();
1492     }
1493     if (paramSelector >= oemCmdStart)
1494     {
1495         return ipmi::responseParmNotSupported();
1496     }
1497 
1498     if (paramSelector == 0)
1499     {
1500         // attempt to set the 'set in progress' value (in parameter #0)
1501         // when not in the set complete state.
1502         if ((transferStatus != setComplete) && (data1 == setInProgress))
1503         {
1504             return ipmi::responseSetInProgressActive();
1505         }
1506         // only following 2 states are supported
1507         if (data1 > setInProgress)
1508         {
1509             lg2::error("illegal SetInProgress status");
1510             return ipmi::responseInvalidFieldRequest();
1511         }
1512 
1513         transferStatus = data1 & progressMask;
1514         return ipmi::responseSuccess();
1515     }
1516 
1517     if (configData.size() > configParameterLength)
1518     {
1519         return ipmi::responseInvalidFieldRequest();
1520     }
1521 
1522     // Append zero's to remaining bytes
1523     if (configData.size() < configParameterLength)
1524     {
1525         fill_n(back_inserter(configData),
1526                (configParameterLength - configData.size()), 0x00);
1527     }
1528 
1529     if (!sysInfoParamStore)
1530     {
1531         sysInfoParamStore = std::make_unique<SysInfoParamStore>();
1532         sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1533                                   sysInfoReadSystemName);
1534     }
1535 
1536     // lookup
1537     std::tuple<bool, std::string> ret =
1538         sysInfoParamStore->lookup(paramSelector);
1539     bool found = std::get<0>(ret);
1540     std::string& paramString = std::get<1>(ret);
1541     if (!found)
1542     {
1543         // parameter does not exist. Init new
1544         paramString = "";
1545     }
1546 
1547     uint8_t setSelector = data1;
1548     size_t count = 0;
1549     if (setSelector == 0) // First chunk has only 14 bytes.
1550     {
1551         uint8_t encoding = configData.at(0);
1552         if (encoding > maxValidEncodingData)
1553         {
1554             return ipmi::responseInvalidFieldRequest();
1555         }
1556 
1557         size_t stringLen = configData.at(1); // string length
1558         count = std::min(stringLen, smallChunkSize);
1559         count = std::min(count, configData.size());
1560         paramString.resize(stringLen); // reserve space
1561         std::copy_n(configData.begin() + configDataOverhead, count,
1562                     paramString.begin());
1563     }
1564     else
1565     {
1566         size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
1567         if (offset >= paramString.length())
1568         {
1569             return ipmi::responseParmOutOfRange();
1570         }
1571         count = std::min(paramString.length() - offset, configData.size());
1572         std::copy_n(configData.begin(), count, paramString.begin() + offset);
1573     }
1574     sysInfoParamStore->update(paramSelector, paramString);
1575     return ipmi::responseSuccess();
1576 }
1577 
1578 #ifdef ENABLE_I2C_WHITELIST_CHECK
convertStringToData(const std::string & command)1579 inline std::vector<uint8_t> convertStringToData(const std::string& command)
1580 {
1581     std::istringstream iss(command);
1582     std::string token;
1583     std::vector<uint8_t> dataValue;
1584     while (std::getline(iss, token, ' '))
1585     {
1586         dataValue.emplace_back(
1587             static_cast<uint8_t>(std::stoul(token, nullptr, base_16)));
1588     }
1589     return dataValue;
1590 }
1591 
populateI2CControllerWRAllowlist()1592 static bool populateI2CControllerWRAllowlist()
1593 {
1594     nlohmann::json data = nullptr;
1595     std::ifstream jsonFile(i2cControllerWRAllowlistFile);
1596 
1597     if (!jsonFile.good())
1598     {
1599         lg2::warning("i2c allow list file not found! file name: {FILE_NAME}",
1600                      "FILE_NAME", i2cControllerWRAllowlistFile);
1601         return false;
1602     }
1603 
1604     try
1605     {
1606         data = nlohmann::json::parse(jsonFile, nullptr, false);
1607     }
1608     catch (const nlohmann::json::parse_error& e)
1609     {
1610         lg2::error("Corrupted i2c allow list config file, "
1611                    "file name: {FILE_NAME}, error: {ERROR}",
1612                    "FILE_NAME", i2cControllerWRAllowlistFile, "ERROR", e);
1613         return false;
1614     }
1615 
1616     try
1617     {
1618         // Example JSON Structure format
1619         // "filters": [
1620         //    {
1621         //      "Description": "Allow full read - ignore first byte write value
1622         //      for 0x40 to 0x4F",
1623         //      "busId": "0x01",
1624         //      "slaveAddr": "0x40",
1625         //      "slaveAddrMask": "0x0F",
1626         //      "command": "0x00",
1627         //      "commandMask": "0xFF"
1628         //    },
1629         //    {
1630         //      "Description": "Allow full read - first byte match 0x05 and
1631         //      ignore second byte",
1632         //      "busId": "0x01",
1633         //      "slaveAddr": "0x57",
1634         //      "slaveAddrMask": "0x00",
1635         //      "command": "0x05 0x00",
1636         //      "commandMask": "0x00 0xFF"
1637         //    },]
1638 
1639         nlohmann::json filters = data[filtersStr].get<nlohmann::json>();
1640         std::vector<i2cControllerWRAllowlist>& allowlist = getWRAllowlist();
1641         for (const auto& it : filters.items())
1642         {
1643             nlohmann::json filter = it.value();
1644             if (filter.is_null())
1645             {
1646                 lg2::error(
1647                     "Corrupted I2C controller write read allowlist config file, "
1648                     "file name: {FILE_NAME}",
1649                     "FILE_NAME", i2cControllerWRAllowlistFile);
1650                 return false;
1651             }
1652             const std::vector<uint8_t>& writeData =
1653                 convertStringToData(filter[cmdStr].get<std::string>());
1654             const std::vector<uint8_t>& writeDataMask =
1655                 convertStringToData(filter[cmdMaskStr].get<std::string>());
1656             if (writeDataMask.size() != writeData.size())
1657             {
1658                 lg2::error("I2C controller write read allowlist filter "
1659                            "mismatch for command & mask size");
1660                 return false;
1661             }
1662             allowlist.push_back(
1663                 {static_cast<uint8_t>(std::stoul(
1664                      filter[busIdStr].get<std::string>(), nullptr, base_16)),
1665                  static_cast<uint8_t>(
1666                      std::stoul(filter[targetAddrStr].get<std::string>(),
1667                                 nullptr, base_16)),
1668                  static_cast<uint8_t>(
1669                      std::stoul(filter[targetAddrMaskStr].get<std::string>(),
1670                                 nullptr, base_16)),
1671                  writeData, writeDataMask});
1672         }
1673         if (allowlist.size() != filters.size())
1674         {
1675             lg2::error(
1676                 "I2C controller write read allowlist filter size mismatch");
1677             return false;
1678         }
1679     }
1680     catch (const std::exception& e)
1681     {
1682         lg2::error("I2C controller write read allowlist "
1683                    "unexpected exception: {ERROR}",
1684                    "ERROR", e);
1685         return false;
1686     }
1687     return true;
1688 }
1689 
isWriteDataAllowlisted(const std::vector<uint8_t> & data,const std::vector<uint8_t> & dataMask,const std::vector<uint8_t> & writeData)1690 static inline bool isWriteDataAllowlisted(const std::vector<uint8_t>& data,
1691                                           const std::vector<uint8_t>& dataMask,
1692                                           const std::vector<uint8_t>& writeData)
1693 {
1694     std::vector<uint8_t> processedDataBuf(data.size());
1695     std::vector<uint8_t> processedReqBuf(dataMask.size());
1696     std::transform(writeData.begin(), writeData.end(), dataMask.begin(),
1697                    processedReqBuf.begin(), std::bit_or<uint8_t>());
1698     std::transform(data.begin(), data.end(), dataMask.begin(),
1699                    processedDataBuf.begin(), std::bit_or<uint8_t>());
1700 
1701     return (processedDataBuf == processedReqBuf);
1702 }
1703 
isCmdAllowlisted(uint8_t busId,uint8_t targetAddr,std::vector<uint8_t> & writeData)1704 static bool isCmdAllowlisted(uint8_t busId, uint8_t targetAddr,
1705                              std::vector<uint8_t>& writeData)
1706 {
1707     std::vector<i2cControllerWRAllowlist>& allowList = getWRAllowlist();
1708     for (const auto& wlEntry : allowList)
1709     {
1710         if ((busId == wlEntry.busId) &&
1711             ((targetAddr | wlEntry.targetAddrMask) ==
1712              (wlEntry.targetAddr | wlEntry.targetAddrMask)))
1713         {
1714             const std::vector<uint8_t>& dataMask = wlEntry.dataMask;
1715             // Skip as no-match, if requested write data is more than the
1716             // write data mask size
1717             if (writeData.size() > dataMask.size())
1718             {
1719                 continue;
1720             }
1721             if (isWriteDataAllowlisted(wlEntry.data, dataMask, writeData))
1722             {
1723                 return true;
1724             }
1725         }
1726     }
1727     return false;
1728 }
1729 #else
populateI2CControllerWRAllowlist()1730 static bool populateI2CControllerWRAllowlist()
1731 {
1732     lg2::info("I2C_WHITELIST_CHECK is disabled, do not populate allowlist");
1733     return true;
1734 }
1735 #endif // ENABLE_I2C_WHITELIST_CHECK
1736 
1737 /** @brief implements controller write read IPMI command which can be used for
1738  * low-level I2C/SMBus write, read or write-read access
1739  *  @param isPrivateBus -to indicate private bus usage
1740  *  @param busId - bus id
1741  *  @param channelNum - channel number
1742  *  @param reserved - skip 1 bit
1743  *  @param targetAddr - target address
1744  *  @param read count - number of bytes to be read
1745  *  @param writeData - data to be written
1746  *
1747  *  @returns IPMI completion code plus response data
1748  *   - readData - i2c response data
1749  */
ipmiControllerWriteRead(bool isPrivateBus,uint3_t busId,uint4_t channelNum,bool reserved,uint7_t targetAddr,uint8_t readCount,std::vector<uint8_t> writeData)1750 ipmi::RspType<std::vector<uint8_t>> ipmiControllerWriteRead(
1751     [[maybe_unused]] bool isPrivateBus, uint3_t busId,
1752     [[maybe_unused]] uint4_t channelNum, bool reserved, uint7_t targetAddr,
1753     uint8_t readCount, std::vector<uint8_t> writeData)
1754 {
1755     if (reserved)
1756     {
1757         return ipmi::responseInvalidFieldRequest();
1758     }
1759     const size_t writeCount = writeData.size();
1760     if (!readCount && !writeCount)
1761     {
1762         lg2::error("Controller write read command: Read & write count are 0");
1763         return ipmi::responseInvalidFieldRequest();
1764     }
1765 #ifdef ENABLE_I2C_WHITELIST_CHECK
1766     if (!isCmdAllowlisted(static_cast<uint8_t>(busId),
1767                           static_cast<uint8_t>(targetAddr), writeData))
1768     {
1769         lg2::error("Controller write read request blocked!, "
1770                    "bus: {BUS}, addr: {ADDR}",
1771                    "BUS", static_cast<uint8_t>(busId), "ADDR", lg2::hex,
1772                    static_cast<uint8_t>(targetAddr));
1773     }
1774 #endif // ENABLE_I2C_WHITELIST_CHECK
1775     std::vector<uint8_t> readBuf(readCount);
1776     std::string i2cBus =
1777         "/dev/i2c-" + std::to_string(static_cast<uint8_t>(busId));
1778 
1779     ipmi::Cc ret = ipmi::i2cWriteRead(i2cBus, static_cast<uint8_t>(targetAddr),
1780                                       writeData, readBuf);
1781     if (ret != ipmi::ccSuccess)
1782     {
1783         return ipmi::response(ret);
1784     }
1785     return ipmi::responseSuccess(readBuf);
1786 }
1787 
registerNetFnAppFunctions()1788 void registerNetFnAppFunctions()
1789 {
1790     // <Get Device ID>
1791     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1792                           ipmi::app::cmdGetDeviceId, ipmi::Privilege::User,
1793                           ipmiAppGetDeviceId);
1794 
1795     // <Get BT Interface Capabilities>
1796     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1797                           ipmi::app::cmdGetBtIfaceCapabilities,
1798                           ipmi::Privilege::User, ipmiAppGetBtCapabilities);
1799 
1800     // <Reset Watchdog Timer>
1801     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1802                           ipmi::app::cmdResetWatchdogTimer,
1803                           ipmi::Privilege::Operator, ipmiAppResetWatchdogTimer);
1804 
1805     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1806                           ipmi::app::cmdGetSessionInfo, ipmi::Privilege::User,
1807                           ipmiAppGetSessionInfo);
1808 
1809     // <Set Watchdog Timer>
1810     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1811                           ipmi::app::cmdSetWatchdogTimer,
1812                           ipmi::Privilege::Operator, ipmiSetWatchdogTimer);
1813 
1814     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1815                           ipmi::app::cmdCloseSession, ipmi::Privilege::Callback,
1816                           ipmiAppCloseSession);
1817 
1818     // <Get Watchdog Timer>
1819     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1820                           ipmi::app::cmdGetWatchdogTimer, ipmi::Privilege::User,
1821                           ipmiGetWatchdogTimer);
1822 
1823     // <Get Self Test Results>
1824     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1825                           ipmi::app::cmdGetSelfTestResults,
1826                           ipmi::Privilege::User, ipmiAppGetSelfTestResults);
1827 
1828     // <Get Device GUID>
1829     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1830                           ipmi::app::cmdGetDeviceGuid, ipmi::Privilege::User,
1831                           ipmiAppGetDeviceGuid);
1832 
1833     // <Set ACPI Power State>
1834     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1835                           ipmi::app::cmdSetAcpiPowerState,
1836                           ipmi::Privilege::Admin, ipmiSetAcpiPowerState);
1837     // <Get ACPI Power State>
1838     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1839                           ipmi::app::cmdGetAcpiPowerState,
1840                           ipmi::Privilege::User, ipmiGetAcpiPowerState);
1841 
1842     // Note: For security reason, this command will be registered only when
1843     // there are proper I2C Controller write read allowlist
1844     if (populateI2CControllerWRAllowlist())
1845     {
1846         // Note: For security reasons, registering controller write read as
1847         // admin privilege command, even though IPMI 2.0 specification allows it
1848         // as operator privilege.
1849         ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1850                               ipmi::app::cmdMasterWriteRead,
1851                               ipmi::Privilege::Admin, ipmiControllerWriteRead);
1852     }
1853 
1854     // <Get System GUID Command>
1855     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1856                           ipmi::app::cmdGetSystemGuid, ipmi::Privilege::User,
1857                           ipmiAppGetSystemGuid);
1858 
1859     // <Get Channel Cipher Suites Command>
1860     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1861                           ipmi::app::cmdGetChannelCipherSuites,
1862                           ipmi::Privilege::None, getChannelCipherSuites);
1863 
1864     // <Get System Info Command>
1865     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1866                           ipmi::app::cmdGetSystemInfoParameters,
1867                           ipmi::Privilege::User, ipmiAppGetSystemInfo);
1868     // <Set System Info Command>
1869     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1870                           ipmi::app::cmdSetSystemInfoParameters,
1871                           ipmi::Privilege::Admin, ipmiAppSetSystemInfo);
1872     return;
1873 }
1874