| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 1 | #include "config.h" | 
|  | 2 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 3 | #include <arpa/inet.h> | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 4 | #include <fcntl.h> | 
| Xo Wang | 8765133 | 2017-08-11 10:17:59 -0700 | [diff] [blame] | 5 | #include <limits.h> | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 6 | #include <linux/i2c-dev.h> | 
|  | 7 | #include <linux/i2c.h> | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 8 | #include <sys/ioctl.h> | 
|  | 9 | #include <sys/stat.h> | 
|  | 10 | #include <sys/types.h> | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 11 | #include <systemd/sd-bus.h> | 
| Xo Wang | 8765133 | 2017-08-11 10:17:59 -0700 | [diff] [blame] | 12 | #include <unistd.h> | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 13 |  | 
| Vernon Mauery | 0120b68 | 2019-03-25 13:08:54 -0700 | [diff] [blame] | 14 | #include <app/channel.hpp> | 
|  | 15 | #include <app/watchdog.hpp> | 
|  | 16 | #include <apphandler.hpp> | 
| Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 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> | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 24 | #include <phosphor-logging/lg2.hpp> | 
| Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 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> | 
| Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 34 | #include <array> | 
| Willy Tu | 886d684 | 2022-06-03 02:50:47 -0700 | [diff] [blame] | 35 | #include <charconv> | 
| Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 36 | #include <cstddef> | 
| Vernon Mauery | 0120b68 | 2019-03-25 13:08:54 -0700 | [diff] [blame] | 37 | #include <cstdint> | 
| Vernon Mauery | bdda800 | 2019-02-26 10:18:51 -0800 | [diff] [blame] | 38 | #include <filesystem> | 
| Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 39 | #include <fstream> | 
|  | 40 | #include <memory> | 
| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 41 | #include <regex> | 
| Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 42 | #include <string> | 
| Willy Tu | 886d684 | 2022-06-03 02:50:47 -0700 | [diff] [blame] | 43 | #include <string_view> | 
| Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 44 | #include <tuple> | 
|  | 45 | #include <vector> | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 46 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 47 | extern sd_bus* bus; | 
| vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 48 |  | 
| Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 49 | constexpr auto bmc_state_interface = "xyz.openbmc_project.State.BMC"; | 
|  | 50 | constexpr auto bmc_state_property = "CurrentBMCState"; | 
| Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 51 |  | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 52 | static constexpr auto redundancyIntf = | 
|  | 53 | "xyz.openbmc_project.Software.RedundancyPriority"; | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 54 | static constexpr auto versionIntf = "xyz.openbmc_project.Software.Version"; | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 55 | static constexpr auto activationIntf = | 
|  | 56 | "xyz.openbmc_project.Software.Activation"; | 
|  | 57 | static constexpr auto softwareRoot = "/xyz/openbmc_project/software"; | 
|  | 58 |  | 
| Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 59 | void register_netfn_app_functions() __attribute__((constructor)); | 
| vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 60 |  | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 61 | using namespace phosphor::logging; | 
| Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 62 | using namespace sdbusplus::error::xyz::openbmc_project::common; | 
|  | 63 | using Version = sdbusplus::server::xyz::openbmc_project::software::Version; | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 64 | using Activation = | 
| Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 65 | sdbusplus::server::xyz::openbmc_project::software::Activation; | 
|  | 66 | using BMC = sdbusplus::server::xyz::openbmc_project::state::BMC; | 
| Vernon Mauery | 185b9f8 | 2018-07-20 10:52:36 -0700 | [diff] [blame] | 67 | namespace fs = std::filesystem; | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 68 |  | 
| Yong Li | bd0503a | 2019-08-22 17:17:17 +0800 | [diff] [blame] | 69 | #ifdef ENABLE_I2C_WHITELIST_CHECK | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 70 | typedef struct | 
|  | 71 | { | 
|  | 72 | uint8_t busId; | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 73 | uint8_t targetAddr; | 
|  | 74 | uint8_t targetAddrMask; | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 75 | std::vector<uint8_t> data; | 
|  | 76 | std::vector<uint8_t> dataMask; | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 77 | } i2cControllerWRAllowlist; | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 78 |  | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 79 | static std::vector<i2cControllerWRAllowlist>& getWRAllowlist() | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 80 | { | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 81 | static std::vector<i2cControllerWRAllowlist> wrAllowlist; | 
|  | 82 | return wrAllowlist; | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 85 | static constexpr const char* i2cControllerWRAllowlistFile = | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 86 | "/usr/share/ipmi-providers/master_write_read_white_list.json"; | 
|  | 87 |  | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 88 | static constexpr const char* filtersStr = "filters"; | 
|  | 89 | static constexpr const char* busIdStr = "busId"; | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 90 | static constexpr const char* targetAddrStr = "slaveAddr"; | 
|  | 91 | static constexpr const char* targetAddrMaskStr = "slaveAddrMask"; | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 92 | static constexpr const char* cmdStr = "command"; | 
|  | 93 | static constexpr const char* cmdMaskStr = "commandMask"; | 
|  | 94 | static constexpr int base_16 = 16; | 
| Yong Li | bd0503a | 2019-08-22 17:17:17 +0800 | [diff] [blame] | 95 | #endif // ENABLE_I2C_WHITELIST_CHECK | 
| jayaprakash Mutyala | 3c5e413 | 2020-04-27 23:00:05 +0000 | [diff] [blame] | 96 | static constexpr uint8_t oemCmdStart = 192; | 
| jayaprakash Mutyala | 3c5e413 | 2020-04-27 23:00:05 +0000 | [diff] [blame] | 97 | static constexpr uint8_t invalidParamSelectorStart = 8; | 
|  | 98 | static constexpr uint8_t invalidParamSelectorEnd = 191; | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 99 |  | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 100 | /** | 
|  | 101 | * @brief Returns the Version info from primary s/w object | 
|  | 102 | * | 
|  | 103 | * Get the Version info from the active s/w object which is having high | 
|  | 104 | * "Priority" value(a smaller number is a higher priority) and "Purpose" | 
|  | 105 | * is "BMC" from the list of all s/w objects those are implementing | 
|  | 106 | * RedundancyPriority interface from the given softwareRoot path. | 
|  | 107 | * | 
|  | 108 | * @return On success returns the Version info from primary s/w object. | 
|  | 109 | * | 
|  | 110 | */ | 
| Vernon Mauery | ea1c401 | 2019-05-24 13:26:16 -0700 | [diff] [blame] | 111 | std::string getActiveSoftwareVersionInfo(ipmi::Context::ptr ctx) | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 112 | { | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 113 | std::string revision{}; | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 114 | ipmi::ObjectTree objectTree; | 
|  | 115 | try | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 116 | { | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 117 | objectTree = | 
|  | 118 | ipmi::getAllDbusObjects(*ctx->bus, softwareRoot, redundancyIntf); | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 119 | } | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 120 | catch (const sdbusplus::exception_t& e) | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 121 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 122 | lg2::error("Failed to fetch redundancy object from dbus, " | 
|  | 123 | "interface: {INTERFACE},  error: {ERROR}", | 
|  | 124 | "INTERFACE", redundancyIntf, "ERROR", e); | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 125 | elog<InternalFailure>(); | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | auto objectFound = false; | 
|  | 129 | for (auto& softObject : objectTree) | 
|  | 130 | { | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 131 | auto service = | 
|  | 132 | ipmi::getService(*ctx->bus, redundancyIntf, softObject.first); | 
|  | 133 | auto objValueTree = | 
|  | 134 | ipmi::getManagedObjects(*ctx->bus, service, softwareRoot); | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 135 |  | 
|  | 136 | auto minPriority = 0xFF; | 
|  | 137 | for (const auto& objIter : objValueTree) | 
|  | 138 | { | 
|  | 139 | try | 
|  | 140 | { | 
|  | 141 | auto& intfMap = objIter.second; | 
|  | 142 | auto& redundancyPriorityProps = intfMap.at(redundancyIntf); | 
|  | 143 | auto& versionProps = intfMap.at(versionIntf); | 
|  | 144 | auto& activationProps = intfMap.at(activationIntf); | 
| Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 145 | auto priority = | 
|  | 146 | std::get<uint8_t>(redundancyPriorityProps.at("Priority")); | 
| William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 147 | auto purpose = | 
| Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 148 | std::get<std::string>(versionProps.at("Purpose")); | 
|  | 149 | auto activation = | 
|  | 150 | std::get<std::string>(activationProps.at("Activation")); | 
| William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 151 | auto version = | 
| Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 152 | std::get<std::string>(versionProps.at("Version")); | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 153 | if ((Version::convertVersionPurposeFromString(purpose) == | 
|  | 154 | Version::VersionPurpose::BMC) && | 
|  | 155 | (Activation::convertActivationsFromString(activation) == | 
|  | 156 | Activation::Activations::Active)) | 
|  | 157 | { | 
|  | 158 | if (priority < minPriority) | 
|  | 159 | { | 
|  | 160 | minPriority = priority; | 
|  | 161 | objectFound = true; | 
|  | 162 | revision = std::move(version); | 
|  | 163 | } | 
|  | 164 | } | 
|  | 165 | } | 
|  | 166 | catch (const std::exception& e) | 
|  | 167 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 168 | lg2::error("error message: {ERROR}", "ERROR", e); | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 169 | } | 
|  | 170 | } | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | if (!objectFound) | 
|  | 174 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 175 | lg2::error("Could not found an BMC software Object"); | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 176 | elog<InternalFailure>(); | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | return revision; | 
|  | 180 | } | 
|  | 181 |  | 
| Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 182 | bool getCurrentBmcState() | 
|  | 183 | { | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 184 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; | 
| Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 185 |  | 
|  | 186 | // Get the Inventory object implementing the BMC interface | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 187 | ipmi::DbusObjectInfo bmcObject = | 
|  | 188 | ipmi::getDbusObject(bus, bmc_state_interface); | 
|  | 189 | auto variant = | 
|  | 190 | ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first, | 
|  | 191 | bmc_state_interface, bmc_state_property); | 
| Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 192 |  | 
| Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 193 | return std::holds_alternative<std::string>(variant) && | 
|  | 194 | BMC::convertBMCStateFromString(std::get<std::string>(variant)) == | 
|  | 195 | BMC::BMCState::Ready; | 
| Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
| Patrick Venture | 94930a1 | 2019-04-30 10:01:58 -0700 | [diff] [blame] | 198 | bool getCurrentBmcStateWithFallback(const bool fallbackAvailability) | 
|  | 199 | { | 
|  | 200 | try | 
|  | 201 | { | 
|  | 202 | return getCurrentBmcState(); | 
|  | 203 | } | 
|  | 204 | catch (...) | 
|  | 205 | { | 
|  | 206 | // Nothing provided the BMC interface, therefore return whatever was | 
|  | 207 | // configured as the default. | 
|  | 208 | return fallbackAvailability; | 
|  | 209 | } | 
|  | 210 | } | 
|  | 211 |  | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 212 | namespace acpi_state | 
|  | 213 | { | 
| Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 214 | using namespace sdbusplus::server::xyz::openbmc_project::control::power; | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 215 |  | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 216 | const static constexpr char* acpiInterface = | 
|  | 217 | "xyz.openbmc_project.Control.Power.ACPIPowerState"; | 
|  | 218 | const static constexpr char* sysACPIProp = "SysACPIStatus"; | 
|  | 219 | const static constexpr char* devACPIProp = "DevACPIStatus"; | 
|  | 220 |  | 
|  | 221 | enum class PowerStateType : uint8_t | 
|  | 222 | { | 
|  | 223 | sysPowerState = 0x00, | 
|  | 224 | devPowerState = 0x01, | 
|  | 225 | }; | 
|  | 226 |  | 
|  | 227 | // Defined in 20.6 of ipmi doc | 
|  | 228 | enum class PowerState : uint8_t | 
|  | 229 | { | 
|  | 230 | s0G0D0 = 0x00, | 
|  | 231 | s1D1 = 0x01, | 
|  | 232 | s2D2 = 0x02, | 
|  | 233 | s3D3 = 0x03, | 
|  | 234 | s4 = 0x04, | 
|  | 235 | s5G2 = 0x05, | 
|  | 236 | s4S5 = 0x06, | 
|  | 237 | g3 = 0x07, | 
|  | 238 | sleep = 0x08, | 
|  | 239 | g1Sleep = 0x09, | 
|  | 240 | override = 0x0a, | 
|  | 241 | legacyOn = 0x20, | 
|  | 242 | legacyOff = 0x21, | 
|  | 243 | unknown = 0x2a, | 
|  | 244 | noChange = 0x7f, | 
|  | 245 | }; | 
|  | 246 |  | 
|  | 247 | static constexpr uint8_t stateChanged = 0x80; | 
|  | 248 |  | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 249 | std::map<ACPIPowerState::ACPI, PowerState> dbusToIPMI = { | 
|  | 250 | {ACPIPowerState::ACPI::S0_G0_D0, PowerState::s0G0D0}, | 
|  | 251 | {ACPIPowerState::ACPI::S1_D1, PowerState::s1D1}, | 
|  | 252 | {ACPIPowerState::ACPI::S2_D2, PowerState::s2D2}, | 
|  | 253 | {ACPIPowerState::ACPI::S3_D3, PowerState::s3D3}, | 
|  | 254 | {ACPIPowerState::ACPI::S4, PowerState::s4}, | 
|  | 255 | {ACPIPowerState::ACPI::S5_G2, PowerState::s5G2}, | 
|  | 256 | {ACPIPowerState::ACPI::S4_S5, PowerState::s4S5}, | 
|  | 257 | {ACPIPowerState::ACPI::G3, PowerState::g3}, | 
|  | 258 | {ACPIPowerState::ACPI::SLEEP, PowerState::sleep}, | 
|  | 259 | {ACPIPowerState::ACPI::G1_SLEEP, PowerState::g1Sleep}, | 
|  | 260 | {ACPIPowerState::ACPI::OVERRIDE, PowerState::override}, | 
|  | 261 | {ACPIPowerState::ACPI::LEGACY_ON, PowerState::legacyOn}, | 
|  | 262 | {ACPIPowerState::ACPI::LEGACY_OFF, PowerState::legacyOff}, | 
|  | 263 | {ACPIPowerState::ACPI::Unknown, PowerState::unknown}}; | 
|  | 264 |  | 
|  | 265 | bool isValidACPIState(acpi_state::PowerStateType type, uint8_t state) | 
|  | 266 | { | 
|  | 267 | if (type == acpi_state::PowerStateType::sysPowerState) | 
|  | 268 | { | 
|  | 269 | if ((state <= static_cast<uint8_t>(acpi_state::PowerState::override)) || | 
|  | 270 | (state == static_cast<uint8_t>(acpi_state::PowerState::legacyOn)) || | 
|  | 271 | (state == | 
|  | 272 | static_cast<uint8_t>(acpi_state::PowerState::legacyOff)) || | 
|  | 273 | (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) || | 
|  | 274 | (state == static_cast<uint8_t>(acpi_state::PowerState::noChange))) | 
|  | 275 | { | 
|  | 276 | return true; | 
|  | 277 | } | 
|  | 278 | else | 
|  | 279 | { | 
|  | 280 | return false; | 
|  | 281 | } | 
|  | 282 | } | 
|  | 283 | else if (type == acpi_state::PowerStateType::devPowerState) | 
|  | 284 | { | 
|  | 285 | if ((state <= static_cast<uint8_t>(acpi_state::PowerState::s3D3)) || | 
|  | 286 | (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) || | 
|  | 287 | (state == static_cast<uint8_t>(acpi_state::PowerState::noChange))) | 
|  | 288 | { | 
|  | 289 | return true; | 
|  | 290 | } | 
|  | 291 | else | 
|  | 292 | { | 
|  | 293 | return false; | 
|  | 294 | } | 
|  | 295 | } | 
|  | 296 | else | 
|  | 297 | { | 
|  | 298 | return false; | 
|  | 299 | } | 
|  | 300 | return false; | 
|  | 301 | } | 
|  | 302 | } // namespace acpi_state | 
|  | 303 |  | 
| Deepak Kumar Sahu | 520c131 | 2019-05-17 18:14:09 +0000 | [diff] [blame] | 304 | /** @brief implements Set ACPI Power State command | 
|  | 305 | * @param sysAcpiState - ACPI system power state to set | 
|  | 306 | * @param devAcpiState - ACPI device power state to set | 
|  | 307 | * | 
|  | 308 | * @return IPMI completion code on success | 
|  | 309 | **/ | 
|  | 310 | ipmi::RspType<> ipmiSetAcpiPowerState(uint8_t sysAcpiState, | 
|  | 311 | uint8_t devAcpiState) | 
| Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 312 | { | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 313 | auto s = static_cast<uint8_t>(acpi_state::PowerState::unknown); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 314 |  | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 315 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 316 |  | 
|  | 317 | auto value = acpi_state::ACPIPowerState::ACPI::Unknown; | 
|  | 318 |  | 
| Deepak Kumar Sahu | 520c131 | 2019-05-17 18:14:09 +0000 | [diff] [blame] | 319 | if (sysAcpiState & acpi_state::stateChanged) | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 320 | { | 
|  | 321 | // set system power state | 
| Deepak Kumar Sahu | 520c131 | 2019-05-17 18:14:09 +0000 | [diff] [blame] | 322 | s = sysAcpiState & ~acpi_state::stateChanged; | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 323 |  | 
|  | 324 | if (!acpi_state::isValidACPIState( | 
|  | 325 | acpi_state::PowerStateType::sysPowerState, s)) | 
|  | 326 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 327 | lg2::error("set_acpi_power sys invalid input, S: {S}", "S", s); | 
| Deepak Kumar Sahu | 520c131 | 2019-05-17 18:14:09 +0000 | [diff] [blame] | 328 | return ipmi::responseParmOutOfRange(); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 329 | } | 
|  | 330 |  | 
|  | 331 | // valid input | 
|  | 332 | if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange)) | 
|  | 333 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 334 | lg2::debug("No change for system power state"); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 335 | } | 
|  | 336 | else | 
|  | 337 | { | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 338 | auto found = std::find_if( | 
|  | 339 | acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(), | 
|  | 340 | [&s](const auto& iter) { | 
|  | 341 | return (static_cast<uint8_t>(iter.second) == s); | 
|  | 342 | }); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 343 |  | 
|  | 344 | value = found->first; | 
|  | 345 |  | 
|  | 346 | try | 
|  | 347 | { | 
|  | 348 | auto acpiObject = | 
|  | 349 | ipmi::getDbusObject(bus, acpi_state::acpiInterface); | 
|  | 350 | ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first, | 
|  | 351 | acpi_state::acpiInterface, | 
|  | 352 | acpi_state::sysACPIProp, | 
|  | 353 | convertForMessage(value)); | 
|  | 354 | } | 
|  | 355 | catch (const InternalFailure& e) | 
|  | 356 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 357 | lg2::error("Failed in set ACPI system property: {ERROR}", | 
|  | 358 | "ERROR", e); | 
| Deepak Kumar Sahu | 520c131 | 2019-05-17 18:14:09 +0000 | [diff] [blame] | 359 | return ipmi::responseUnspecifiedError(); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 360 | } | 
|  | 361 | } | 
|  | 362 | } | 
|  | 363 | else | 
|  | 364 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 365 | lg2::debug("Do not change system power state"); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 366 | } | 
|  | 367 |  | 
| Deepak Kumar Sahu | 520c131 | 2019-05-17 18:14:09 +0000 | [diff] [blame] | 368 | if (devAcpiState & acpi_state::stateChanged) | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 369 | { | 
|  | 370 | // set device power state | 
| Deepak Kumar Sahu | 520c131 | 2019-05-17 18:14:09 +0000 | [diff] [blame] | 371 | s = devAcpiState & ~acpi_state::stateChanged; | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 372 | if (!acpi_state::isValidACPIState( | 
|  | 373 | acpi_state::PowerStateType::devPowerState, s)) | 
|  | 374 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 375 | lg2::error("set_acpi_power dev invalid input, S: {S}", "S", s); | 
| Deepak Kumar Sahu | 520c131 | 2019-05-17 18:14:09 +0000 | [diff] [blame] | 376 | return ipmi::responseParmOutOfRange(); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 377 | } | 
|  | 378 |  | 
|  | 379 | // valid input | 
|  | 380 | if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange)) | 
|  | 381 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 382 | lg2::debug("No change for device power state"); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 383 | } | 
|  | 384 | else | 
|  | 385 | { | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 386 | auto found = std::find_if( | 
|  | 387 | acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(), | 
|  | 388 | [&s](const auto& iter) { | 
|  | 389 | return (static_cast<uint8_t>(iter.second) == s); | 
|  | 390 | }); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 391 |  | 
|  | 392 | value = found->first; | 
|  | 393 |  | 
|  | 394 | try | 
|  | 395 | { | 
|  | 396 | auto acpiObject = | 
|  | 397 | ipmi::getDbusObject(bus, acpi_state::acpiInterface); | 
|  | 398 | ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first, | 
|  | 399 | acpi_state::acpiInterface, | 
|  | 400 | acpi_state::devACPIProp, | 
|  | 401 | convertForMessage(value)); | 
|  | 402 | } | 
|  | 403 | catch (const InternalFailure& e) | 
|  | 404 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 405 | lg2::error("Failed in set ACPI device property: {ERROR}", | 
|  | 406 | "ERROR", e); | 
| Deepak Kumar Sahu | 520c131 | 2019-05-17 18:14:09 +0000 | [diff] [blame] | 407 | return ipmi::responseUnspecifiedError(); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 408 | } | 
|  | 409 | } | 
|  | 410 | } | 
|  | 411 | else | 
|  | 412 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 413 | lg2::debug("Do not change device power state"); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 414 | } | 
| Deepak Kumar Sahu | 520c131 | 2019-05-17 18:14:09 +0000 | [diff] [blame] | 415 | return ipmi::responseSuccess(); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 416 | } | 
|  | 417 |  | 
| Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 418 | /** | 
|  | 419 | *  @brief implements the get ACPI power state command | 
|  | 420 | * | 
|  | 421 | *  @return IPMI completion code plus response data on success. | 
|  | 422 | *   -  ACPI system power state | 
|  | 423 | *   -  ACPI device power state | 
|  | 424 | **/ | 
|  | 425 | ipmi::RspType<uint8_t, // acpiSystemPowerState | 
|  | 426 | uint8_t  // acpiDevicePowerState | 
|  | 427 | > | 
|  | 428 | ipmiGetAcpiPowerState() | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 429 | { | 
| Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 430 | uint8_t sysAcpiState; | 
|  | 431 | uint8_t devAcpiState; | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 432 |  | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 433 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 434 |  | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 435 | try | 
|  | 436 | { | 
|  | 437 | auto acpiObject = ipmi::getDbusObject(bus, acpi_state::acpiInterface); | 
|  | 438 |  | 
|  | 439 | auto sysACPIVal = ipmi::getDbusProperty( | 
|  | 440 | bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface, | 
|  | 441 | acpi_state::sysACPIProp); | 
|  | 442 | auto sysACPI = acpi_state::ACPIPowerState::convertACPIFromString( | 
| Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 443 | std::get<std::string>(sysACPIVal)); | 
| Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 444 | sysAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(sysACPI)); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 445 |  | 
|  | 446 | auto devACPIVal = ipmi::getDbusProperty( | 
|  | 447 | bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface, | 
|  | 448 | acpi_state::devACPIProp); | 
|  | 449 | auto devACPI = acpi_state::ACPIPowerState::convertACPIFromString( | 
| Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 450 | std::get<std::string>(devACPIVal)); | 
| Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 451 | devAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(devACPI)); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 452 | } | 
|  | 453 | catch (const InternalFailure& e) | 
|  | 454 | { | 
| Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 455 | return ipmi::responseUnspecifiedError(); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 456 | } | 
| Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 457 |  | 
|  | 458 | return ipmi::responseSuccess(sysAcpiState, devAcpiState); | 
| Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 459 | } | 
|  | 460 |  | 
| Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 461 | typedef struct | 
|  | 462 | { | 
|  | 463 | char major; | 
|  | 464 | char minor; | 
| Potin Lai | c7c5592 | 2023-02-16 10:33:55 +0800 | [diff] [blame] | 465 | uint8_t aux[4]; | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 466 | } Revision; | 
| Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 467 |  | 
| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 468 | /* Use regular expression searching matched pattern X.Y, and convert it to  */ | 
|  | 469 | /* Major (X) and Minor (Y) version.                                         */ | 
|  | 470 | /* Example:                                                                 */ | 
|  | 471 | /* version = 2.14.0-dev                                                     */ | 
|  | 472 | /*           ^ ^                                                            */ | 
|  | 473 | /*           | |---------------- Minor                                      */ | 
|  | 474 | /*           |------------------ Major                                      */ | 
|  | 475 | /*                                                                          */ | 
| Potin Lai | c7c5592 | 2023-02-16 10:33:55 +0800 | [diff] [blame] | 476 | /* Default regex string only tries to match Major and Minor version.        */ | 
|  | 477 | /*                                                                          */ | 
|  | 478 | /* To match more firmware version info, platforms need to define it own     */ | 
|  | 479 | /* regex string to match more strings, and assign correct mapping index in  */ | 
|  | 480 | /* matches array.                                                           */ | 
|  | 481 | /*                                                                          */ | 
|  | 482 | /* matches[0]: matched index for major ver                                  */ | 
|  | 483 | /* matches[1]: matched index for minor ver                                  */ | 
|  | 484 | /* matches[2]: matched index for aux[0] (set 0 to skip)                     */ | 
|  | 485 | /* matches[3]: matched index for aux[1] (set 0 to skip)                     */ | 
|  | 486 | /* matches[4]: matched index for aux[2] (set 0 to skip)                     */ | 
|  | 487 | /* matches[5]: matched index for aux[3] (set 0 to skip)                     */ | 
|  | 488 | /* Example:                                                                 */ | 
|  | 489 | /* regex = "([\d]+).([\d]+).([\d]+)-dev-([\d]+)-g([0-9a-fA-F]{2})           */ | 
|  | 490 | /*          ([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})"               */ | 
|  | 491 | /* matches = {1,2,5,6,7,8}                                                  */ | 
|  | 492 | /* version = 2.14.0-dev-750-g37a7c5ad1-dirty                                */ | 
|  | 493 | /*           ^ ^  ^     ^    ^ ^ ^ ^                                        */ | 
|  | 494 | /*           | |  |     |    | | | |                                        */ | 
|  | 495 | /*           | |  |     |    | | | |-- Aux byte 3 (0xAD), index 8           */ | 
|  | 496 | /*           | |  |     |    | | |---- Aux byte 2 (0xC5), index 7           */ | 
|  | 497 | /*           | |  |     |    | |------ Aux byte 1 (0xA7), index 6           */ | 
|  | 498 | /*           | |  |     |    |-------- Aux byte 0 (0x37), index 5           */ | 
|  | 499 | /*           | |  |     |------------- Not used, index 4                    */ | 
|  | 500 | /*           | |  |------------------- Not used, index 3                    */ | 
|  | 501 | /*           | |---------------------- Minor (14), index 2                  */ | 
|  | 502 | /*           |------------------------ Major (2), index 1                   */ | 
| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 503 | int convertVersion(std::string s, Revision& rev) | 
| Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 504 | { | 
| Potin Lai | c7c5592 | 2023-02-16 10:33:55 +0800 | [diff] [blame] | 505 | static const std::vector<size_t> matches = { | 
|  | 506 | MAJOR_MATCH_INDEX, MINOR_MATCH_INDEX, AUX_0_MATCH_INDEX, | 
|  | 507 | AUX_1_MATCH_INDEX, AUX_2_MATCH_INDEX, AUX_3_MATCH_INDEX}; | 
| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 508 | std::regex fw_regex(FW_VER_REGEX); | 
|  | 509 | std::smatch m; | 
|  | 510 | Revision r = {0}; | 
|  | 511 | size_t val; | 
| Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 512 |  | 
| Potin Lai | dcde04e | 2023-03-16 18:44:15 +0800 | [diff] [blame] | 513 | if (std::regex_search(s, m, fw_regex)) | 
| Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 514 | { | 
| Potin Lai | c7c5592 | 2023-02-16 10:33:55 +0800 | [diff] [blame] | 515 | if (m.size() < *std::max_element(matches.begin(), matches.end())) | 
|  | 516 | { // max index higher than match count | 
| Potin Lai | dcde04e | 2023-03-16 18:44:15 +0800 | [diff] [blame] | 517 | return -1; | 
|  | 518 | } | 
|  | 519 |  | 
| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 520 | // convert major | 
|  | 521 | { | 
| Potin Lai | c7c5592 | 2023-02-16 10:33:55 +0800 | [diff] [blame] | 522 | std::string_view str = m[matches[0]].str(); | 
| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 523 | auto [ptr, ec]{std::from_chars(str.begin(), str.end(), val)}; | 
|  | 524 | if (ec != std::errc() || ptr != str.begin() + str.size()) | 
|  | 525 | { // failed to convert major string | 
| Potin Lai | dcde04e | 2023-03-16 18:44:15 +0800 | [diff] [blame] | 526 | return -1; | 
| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 527 | } | 
| Potin Lai | 058e291 | 2023-09-07 10:16:38 +0800 | [diff] [blame] | 528 |  | 
|  | 529 | if (val >= 2000) | 
|  | 530 | { // For the platforms use year as major version, it would expect to | 
|  | 531 | // have major version between 0 - 99. If the major version is | 
|  | 532 | // greater than or equal to 2000, it is treated as a year and | 
|  | 533 | // converted to 0 - 99. | 
|  | 534 | r.major = val % 100; | 
|  | 535 | } | 
|  | 536 | else | 
|  | 537 | { | 
|  | 538 | r.major = val & 0x7F; | 
|  | 539 | } | 
| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 540 | } | 
|  | 541 |  | 
|  | 542 | // convert minor | 
|  | 543 | { | 
| Potin Lai | c7c5592 | 2023-02-16 10:33:55 +0800 | [diff] [blame] | 544 | std::string_view str = m[matches[1]].str(); | 
| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 545 | auto [ptr, ec]{std::from_chars(str.begin(), str.end(), val)}; | 
|  | 546 | if (ec != std::errc() || ptr != str.begin() + str.size()) | 
|  | 547 | { // failed to convert minor string | 
| Potin Lai | dcde04e | 2023-03-16 18:44:15 +0800 | [diff] [blame] | 548 | return -1; | 
| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 549 | } | 
|  | 550 | r.minor = val & 0xFF; | 
|  | 551 | } | 
|  | 552 |  | 
| Potin Lai | c7c5592 | 2023-02-16 10:33:55 +0800 | [diff] [blame] | 553 | // convert aux bytes | 
|  | 554 | { | 
|  | 555 | size_t i; | 
|  | 556 | for (i = 0; i < 4; i++) | 
|  | 557 | { | 
|  | 558 | if (matches[i + 2] == 0) | 
|  | 559 | { | 
|  | 560 | continue; | 
|  | 561 | } | 
|  | 562 |  | 
|  | 563 | std::string_view str = m[matches[i + 2]].str(); | 
|  | 564 | auto [ptr, | 
|  | 565 | ec]{std::from_chars(str.begin(), str.end(), val, 16)}; | 
|  | 566 | if (ec != std::errc() || ptr != str.begin() + str.size()) | 
|  | 567 | { // failed to convert aux byte string | 
|  | 568 | break; | 
|  | 569 | } | 
|  | 570 |  | 
|  | 571 | r.aux[i] = val & 0xFF; | 
|  | 572 | } | 
|  | 573 |  | 
|  | 574 | if (i != 4) | 
|  | 575 | { // something wrong durign converting aux bytes | 
|  | 576 | return -1; | 
|  | 577 | } | 
|  | 578 | } | 
|  | 579 |  | 
| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 580 | // all matched | 
|  | 581 | rev = r; | 
|  | 582 | return 0; | 
| Chris Austen | 176c965 | 2016-04-30 16:32:17 -0500 | [diff] [blame] | 583 | } | 
| Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 584 |  | 
| Potin Lai | 5d21420 | 2023-01-16 18:11:49 +0800 | [diff] [blame] | 585 | return -1; | 
| Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 586 | } | 
|  | 587 |  | 
| Vernon Mauery | ea1c401 | 2019-05-24 13:26:16 -0700 | [diff] [blame] | 588 | /* @brief: Implement the Get Device ID IPMI command per the IPMI spec | 
|  | 589 | *  @param[in] ctx - shared_ptr to an IPMI context struct | 
|  | 590 | * | 
|  | 591 | *  @returns IPMI completion code plus response data | 
|  | 592 | *   - Device ID (manufacturer defined) | 
|  | 593 | *   - Device revision[4 bits]; reserved[3 bits]; SDR support[1 bit] | 
|  | 594 | *   - FW revision major[7 bits] (binary encoded); available[1 bit] | 
|  | 595 | *   - FW Revision minor (BCD encoded) | 
|  | 596 | *   - IPMI version (0x02 for IPMI 2.0) | 
|  | 597 | *   - device support (bitfield of supported options) | 
|  | 598 | *   - MFG IANA ID (3 bytes) | 
|  | 599 | *   - product ID (2 bytes) | 
|  | 600 | *   - AUX info (4 bytes) | 
|  | 601 | */ | 
|  | 602 | ipmi::RspType<uint8_t,  // Device ID | 
|  | 603 | uint8_t,  // Device Revision | 
|  | 604 | uint8_t,  // Firmware Revision Major | 
|  | 605 | uint8_t,  // Firmware Revision minor | 
|  | 606 | uint8_t,  // IPMI version | 
|  | 607 | uint8_t,  // Additional device support | 
|  | 608 | uint24_t, // MFG ID | 
|  | 609 | uint16_t, // Product ID | 
|  | 610 | uint32_t  // AUX info | 
|  | 611 | > | 
| Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 612 | ipmiAppGetDeviceId([[maybe_unused]] ipmi::Context::ptr ctx) | 
| Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 613 | { | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 614 | static struct | 
|  | 615 | { | 
|  | 616 | uint8_t id; | 
|  | 617 | uint8_t revision; | 
|  | 618 | uint8_t fw[2]; | 
|  | 619 | uint8_t ipmiVer; | 
|  | 620 | uint8_t addnDevSupport; | 
|  | 621 | uint24_t manufId; | 
|  | 622 | uint16_t prodId; | 
|  | 623 | uint32_t aux; | 
|  | 624 | } devId; | 
| David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 625 | static bool dev_id_initialized = false; | 
| Patrick Venture | 94930a1 | 2019-04-30 10:01:58 -0700 | [diff] [blame] | 626 | static bool defaultActivationSetting = true; | 
| David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 627 | const char* filename = "/usr/share/ipmi-providers/dev_id.json"; | 
| Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 628 | constexpr auto ipmiDevIdStateShift = 7; | 
|  | 629 | constexpr auto ipmiDevIdFw1Mask = ~(1 << ipmiDevIdStateShift); | 
| Willy Tu | b78184e | 2022-10-27 22:57:38 +0000 | [diff] [blame] | 630 |  | 
|  | 631 | #ifdef GET_DBUS_ACTIVE_SOFTWARE | 
|  | 632 | static bool haveBMCVersion = false; | 
| JeffLin | 27a62ec | 2021-11-24 15:40:33 +0800 | [diff] [blame] | 633 | if (!haveBMCVersion || !dev_id_initialized) | 
| David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 634 | { | 
| Willy Tu | b78184e | 2022-10-27 22:57:38 +0000 | [diff] [blame] | 635 | int r = -1; | 
|  | 636 | Revision rev = {0, 0, 0, 0}; | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 637 | try | 
|  | 638 | { | 
| Vernon Mauery | ea1c401 | 2019-05-24 13:26:16 -0700 | [diff] [blame] | 639 | auto version = getActiveSoftwareVersionInfo(ctx); | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 640 | r = convertVersion(version, rev); | 
| David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 641 | } | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 642 | catch (const std::exception& e) | 
|  | 643 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 644 | lg2::error("error message: {ERROR}", "ERROR", e); | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 645 | } | 
| Nan Li | ee0cb90 | 2016-07-11 15:38:03 +0800 | [diff] [blame] | 646 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 647 | if (r >= 0) | 
|  | 648 | { | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 649 | // bit7 identifies if the device is available | 
|  | 650 | // 0=normal operation | 
|  | 651 | // 1=device firmware, SDR update, | 
|  | 652 | // or self-initialization in progress. | 
| Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 653 | // The availability may change in run time, so mask here | 
|  | 654 | // and initialize later. | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 655 | devId.fw[0] = rev.major & ipmiDevIdFw1Mask; | 
| Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 656 |  | 
|  | 657 | rev.minor = (rev.minor > 99 ? 99 : rev.minor); | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 658 | devId.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16; | 
| Potin Lai | c7c5592 | 2023-02-16 10:33:55 +0800 | [diff] [blame] | 659 | std::memcpy(&devId.aux, rev.aux, sizeof(rev.aux)); | 
| Brandon Kim | c1f5aca | 2022-03-17 17:54:06 -0700 | [diff] [blame] | 660 | haveBMCVersion = true; | 
| David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 661 | } | 
| JeffLin | 27a62ec | 2021-11-24 15:40:33 +0800 | [diff] [blame] | 662 | } | 
| Willy Tu | b78184e | 2022-10-27 22:57:38 +0000 | [diff] [blame] | 663 | #endif | 
| JeffLin | 27a62ec | 2021-11-24 15:40:33 +0800 | [diff] [blame] | 664 | if (!dev_id_initialized) | 
|  | 665 | { | 
| David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 666 | // IPMI Spec version 2.0 | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 667 | devId.ipmiVer = 2; | 
| Adriana Kobylak | 0e91264 | 2016-06-22 16:54:39 -0500 | [diff] [blame] | 668 |  | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 669 | std::ifstream devIdFile(filename); | 
|  | 670 | if (devIdFile.is_open()) | 
| David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 671 | { | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 672 | auto data = nlohmann::json::parse(devIdFile, nullptr, false); | 
| David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 673 | if (!data.is_discarded()) | 
|  | 674 | { | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 675 | devId.id = data.value("id", 0); | 
|  | 676 | devId.revision = data.value("revision", 0); | 
|  | 677 | devId.addnDevSupport = data.value("addn_dev_support", 0); | 
|  | 678 | devId.manufId = data.value("manuf_id", 0); | 
|  | 679 | devId.prodId = data.value("prod_id", 0); | 
| Potin Lai | 9a68636 | 2023-09-05 09:27:53 +0800 | [diff] [blame] | 680 | #ifdef GET_DBUS_ACTIVE_SOFTWARE | 
|  | 681 | if (!(AUX_0_MATCH_INDEX || AUX_1_MATCH_INDEX || | 
|  | 682 | AUX_2_MATCH_INDEX || AUX_3_MATCH_INDEX)) | 
|  | 683 | #endif | 
|  | 684 | { | 
|  | 685 | devId.aux = data.value("aux", 0); | 
|  | 686 | } | 
| David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 687 |  | 
| Willy Tu | bfd3a17 | 2022-05-31 13:57:54 -0700 | [diff] [blame] | 688 | if (data.contains("firmware_revision")) | 
|  | 689 | { | 
|  | 690 | const auto& firmwareRevision = data.at("firmware_revision"); | 
|  | 691 | if (firmwareRevision.contains("major")) | 
|  | 692 | { | 
|  | 693 | firmwareRevision.at("major").get_to(devId.fw[0]); | 
|  | 694 | } | 
|  | 695 | if (firmwareRevision.contains("minor")) | 
|  | 696 | { | 
|  | 697 | firmwareRevision.at("minor").get_to(devId.fw[1]); | 
|  | 698 | } | 
|  | 699 | } | 
|  | 700 |  | 
| Patrick Venture | 94930a1 | 2019-04-30 10:01:58 -0700 | [diff] [blame] | 701 | // Set the availablitity of the BMC. | 
|  | 702 | defaultActivationSetting = data.value("availability", true); | 
|  | 703 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 704 | // Don't read the file every time if successful | 
| David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 705 | dev_id_initialized = true; | 
|  | 706 | } | 
|  | 707 | else | 
|  | 708 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 709 | lg2::error("Device ID JSON parser failure"); | 
| Vernon Mauery | f2587fc | 2019-04-09 14:28:15 -0700 | [diff] [blame] | 710 | return ipmi::responseUnspecifiedError(); | 
| David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 711 | } | 
|  | 712 | } | 
|  | 713 | else | 
|  | 714 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 715 | lg2::error("Device ID file not found"); | 
| Vernon Mauery | f2587fc | 2019-04-09 14:28:15 -0700 | [diff] [blame] | 716 | return ipmi::responseUnspecifiedError(); | 
| Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 717 | } | 
|  | 718 | } | 
| Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 719 |  | 
| Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 720 | // Set availability to the actual current BMC state | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 721 | devId.fw[0] &= ipmiDevIdFw1Mask; | 
| Patrick Venture | 94930a1 | 2019-04-30 10:01:58 -0700 | [diff] [blame] | 722 | if (!getCurrentBmcStateWithFallback(defaultActivationSetting)) | 
| Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 723 | { | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 724 | devId.fw[0] |= (1 << ipmiDevIdStateShift); | 
| Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 725 | } | 
|  | 726 |  | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 727 | return ipmi::responseSuccess( | 
|  | 728 | devId.id, devId.revision, devId.fw[0], devId.fw[1], devId.ipmiVer, | 
|  | 729 | devId.addnDevSupport, devId.manufId, devId.prodId, devId.aux); | 
| Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 730 | } | 
|  | 731 |  | 
| Vernon Mauery | b84a528 | 2019-03-25 13:39:03 -0700 | [diff] [blame] | 732 | auto ipmiAppGetSelfTestResults() -> ipmi::RspType<uint8_t, uint8_t> | 
| Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 733 | { | 
| Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 734 | // Byte 2: | 
|  | 735 | //  55h - No error. | 
| Gunnar Mills | 8991dd6 | 2017-10-25 17:11:29 -0500 | [diff] [blame] | 736 | //  56h - Self Test function not implemented in this controller. | 
| Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 737 | //  57h - Corrupted or inaccesssible data or devices. | 
|  | 738 | //  58h - Fatal hardware error. | 
|  | 739 | //  FFh - reserved. | 
|  | 740 | //  all other: Device-specific 'internal failure'. | 
|  | 741 | //  Byte 3: | 
|  | 742 | //      For byte 2 = 55h, 56h, FFh:     00h | 
|  | 743 | //      For byte 2 = 58h, all other:    Device-specific | 
|  | 744 | //      For byte 2 = 57h:   self-test error bitfield. | 
|  | 745 | //      Note: returning 57h does not imply that all test were run. | 
|  | 746 | //      [7] 1b = Cannot access SEL device. | 
|  | 747 | //      [6] 1b = Cannot access SDR Repository. | 
|  | 748 | //      [5] 1b = Cannot access BMC FRU device. | 
|  | 749 | //      [4] 1b = IPMB signal lines do not respond. | 
|  | 750 | //      [3] 1b = SDR Repository empty. | 
|  | 751 | //      [2] 1b = Internal Use Area of BMC FRU corrupted. | 
|  | 752 | //      [1] 1b = controller update 'boot block' firmware corrupted. | 
|  | 753 | //      [0] 1b = controller operational firmware corrupted. | 
| Vernon Mauery | b84a528 | 2019-03-25 13:39:03 -0700 | [diff] [blame] | 754 | constexpr uint8_t notImplemented = 0x56; | 
|  | 755 | constexpr uint8_t zero = 0; | 
|  | 756 | return ipmi::responseSuccess(notImplemented, zero); | 
| Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 757 | } | 
|  | 758 |  | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 759 | static constexpr size_t uuidBinaryLength = 16; | 
|  | 760 | static std::array<uint8_t, uuidBinaryLength> rfc4122ToIpmi(std::string rfc4122) | 
| Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 761 | { | 
| Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 762 | using Argument = xyz::openbmc_project::common::InvalidArgument; | 
| Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 763 | // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223 | 
| Patrick Venture | d211702 | 2018-02-06 08:54:37 -0800 | [diff] [blame] | 764 | // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte | 
|  | 765 | // order | 
| Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 766 | // Ex: 0x2332fc2c40e66298e511f2782395a361 | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 767 | constexpr size_t uuidHexLength = (2 * uuidBinaryLength); | 
|  | 768 | constexpr size_t uuidRfc4122Length = (uuidHexLength + 4); | 
|  | 769 | std::array<uint8_t, uuidBinaryLength> uuid; | 
|  | 770 | if (rfc4122.size() == uuidRfc4122Length) | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 771 | { | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 772 | rfc4122.erase(std::remove(rfc4122.begin(), rfc4122.end(), '-'), | 
|  | 773 | rfc4122.end()); | 
| Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 774 | } | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 775 | if (rfc4122.size() != uuidHexLength) | 
| vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 776 | { | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 777 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"), | 
|  | 778 | Argument::ARGUMENT_VALUE(rfc4122.c_str())); | 
| vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 779 | } | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 780 | for (size_t ind = 0; ind < uuidHexLength; ind += 2) | 
| vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 781 | { | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 782 | char v[3]; | 
|  | 783 | v[0] = rfc4122[ind]; | 
|  | 784 | v[1] = rfc4122[ind + 1]; | 
|  | 785 | v[2] = 0; | 
|  | 786 | size_t err; | 
|  | 787 | long b; | 
|  | 788 | try | 
| Emily Shaffer | edb8bb0 | 2018-09-27 14:50:15 -0700 | [diff] [blame] | 789 | { | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 790 | b = std::stoul(v, &err, 16); | 
| Emily Shaffer | edb8bb0 | 2018-09-27 14:50:15 -0700 | [diff] [blame] | 791 | } | 
| Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 792 | catch (const std::exception& e) | 
| Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 793 | { | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 794 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"), | 
|  | 795 | Argument::ARGUMENT_VALUE(rfc4122.c_str())); | 
| Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 796 | } | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 797 | // check that exactly two ascii bytes were converted | 
|  | 798 | if (err != 2) | 
|  | 799 | { | 
|  | 800 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"), | 
|  | 801 | Argument::ARGUMENT_VALUE(rfc4122.c_str())); | 
|  | 802 | } | 
|  | 803 | uuid[uuidBinaryLength - (ind / 2) - 1] = static_cast<uint8_t>(b); | 
| Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 804 | } | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 805 | return uuid; | 
|  | 806 | } | 
| Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 807 |  | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 808 | auto ipmiAppGetDeviceGuid() | 
|  | 809 | -> ipmi::RspType<std::array<uint8_t, uuidBinaryLength>> | 
|  | 810 | { | 
|  | 811 | // return a fixed GUID based on /etc/machine-id | 
|  | 812 | // This should match the /redfish/v1/Managers/bmc's UUID data | 
| Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 813 |  | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 814 | // machine specific application ID (for BMC ID) | 
|  | 815 | // generated by systemd-id128 -p new as per man page | 
|  | 816 | static constexpr sd_id128_t bmcUuidAppId = SD_ID128_MAKE( | 
|  | 817 | e0, e1, 73, 76, 64, 61, 47, da, a5, 0c, d0, cc, 64, 12, 45, 78); | 
| Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 818 |  | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 819 | sd_id128_t bmcUuid; | 
|  | 820 | // create the UUID from /etc/machine-id via the systemd API | 
|  | 821 | sd_id128_get_machine_app_specific(bmcUuidAppId, &bmcUuid); | 
| Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 822 |  | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 823 | char bmcUuidCstr[SD_ID128_STRING_MAX]; | 
|  | 824 | std::string systemUuid = sd_id128_to_string(bmcUuid, bmcUuidCstr); | 
|  | 825 |  | 
|  | 826 | std::array<uint8_t, uuidBinaryLength> uuid = rfc4122ToIpmi(systemUuid); | 
|  | 827 | return ipmi::responseSuccess(uuid); | 
| Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 828 | } | 
| Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 829 |  | 
| Vernon Mauery | cc99ba4 | 2019-03-25 13:40:11 -0700 | [diff] [blame] | 830 | auto ipmiAppGetBtCapabilities() | 
|  | 831 | -> ipmi::RspType<uint8_t, uint8_t, uint8_t, uint8_t, uint8_t> | 
| vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 832 | { | 
| Adriana Kobylak | 88ad815 | 2016-12-13 10:09:08 -0600 | [diff] [blame] | 833 | // Per IPMI 2.0 spec, the input and output buffer size must be the max | 
|  | 834 | // buffer size minus one byte to allocate space for the length byte. | 
| Vernon Mauery | cc99ba4 | 2019-03-25 13:40:11 -0700 | [diff] [blame] | 835 | constexpr uint8_t nrOutstanding = 0x01; | 
|  | 836 | constexpr uint8_t inputBufferSize = MAX_IPMI_BUFFER - 1; | 
|  | 837 | constexpr uint8_t outputBufferSize = MAX_IPMI_BUFFER - 1; | 
|  | 838 | constexpr uint8_t transactionTime = 0x0A; | 
|  | 839 | constexpr uint8_t nrRetries = 0x01; | 
| vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 840 |  | 
| Vernon Mauery | cc99ba4 | 2019-03-25 13:40:11 -0700 | [diff] [blame] | 841 | return ipmi::responseSuccess(nrOutstanding, inputBufferSize, | 
|  | 842 | outputBufferSize, transactionTime, nrRetries); | 
| vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 843 | } | 
|  | 844 |  | 
| Vernon Mauery | 6c44a94 | 2023-07-27 11:00:10 -0700 | [diff] [blame] | 845 | auto ipmiAppGetSystemGuid(ipmi::Context::ptr& ctx) | 
|  | 846 | -> ipmi::RspType<std::array<uint8_t, 16>> | 
| Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 847 | { | 
| Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 848 | static constexpr auto uuidInterface = "xyz.openbmc_project.Common.UUID"; | 
|  | 849 | static constexpr auto uuidProperty = "UUID"; | 
| Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 850 |  | 
| Vernon Mauery | 6c44a94 | 2023-07-27 11:00:10 -0700 | [diff] [blame] | 851 | // Get the Inventory object implementing BMC interface | 
|  | 852 | ipmi::DbusObjectInfo objectInfo{}; | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 853 | boost::system::error_code ec = | 
|  | 854 | ipmi::getDbusObject(ctx, uuidInterface, objectInfo); | 
| Vernon Mauery | 6c44a94 | 2023-07-27 11:00:10 -0700 | [diff] [blame] | 855 | if (ec.value()) | 
| Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 856 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 857 | lg2::error("Failed to locate System UUID object, " | 
|  | 858 | "interface: {INTERFACE}, error: {ERROR}", | 
|  | 859 | "INTERFACE", uuidInterface, "ERROR", ec.message()); | 
| Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 860 | } | 
| Vernon Mauery | 6c44a94 | 2023-07-27 11:00:10 -0700 | [diff] [blame] | 861 |  | 
|  | 862 | // Read UUID property value from bmcObject | 
|  | 863 | // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223 | 
|  | 864 | std::string rfc4122Uuid{}; | 
|  | 865 | ec = ipmi::getDbusProperty(ctx, objectInfo.second, objectInfo.first, | 
|  | 866 | uuidInterface, uuidProperty, rfc4122Uuid); | 
|  | 867 | if (ec.value()) | 
| Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 868 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 869 | lg2::error("Failed to read System UUID property, " | 
|  | 870 | "interface: {INTERFACE}, property: {PROPERTY}, " | 
|  | 871 | "error: {ERROR}", | 
|  | 872 | "INTERFACE", uuidInterface, "PROPERTY", uuidProperty, | 
|  | 873 | "ERROR", ec.message()); | 
| Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 874 | return ipmi::responseUnspecifiedError(); | 
| Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 875 | } | 
| Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 876 | std::array<uint8_t, 16> uuid; | 
| Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 877 | try | 
|  | 878 | { | 
|  | 879 | // convert to IPMI format | 
|  | 880 | uuid = rfc4122ToIpmi(rfc4122Uuid); | 
|  | 881 | } | 
|  | 882 | catch (const InvalidArgument& e) | 
|  | 883 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 884 | lg2::error("Failed in parsing BMC UUID property, " | 
|  | 885 | "interface: {INTERFACE}, property: {PROPERTY}, " | 
|  | 886 | "value: {VALUE}, error: {ERROR}", | 
|  | 887 | "INTERFACE", uuidInterface, "PROPERTY", uuidProperty, | 
|  | 888 | "VALUE", rfc4122Uuid, "ERROR", e); | 
| Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 889 | return ipmi::responseUnspecifiedError(); | 
|  | 890 | } | 
|  | 891 | return ipmi::responseSuccess(uuid); | 
| Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 892 | } | 
|  | 893 |  | 
| Rajashekar Gade Reddy | e702392 | 2019-07-10 16:54:55 +0000 | [diff] [blame] | 894 | /** | 
|  | 895 | * @brief set the session state as teardown | 
|  | 896 | * | 
|  | 897 | * This function is to set the session state to tear down in progress if the | 
|  | 898 | * state is active. | 
|  | 899 | * | 
|  | 900 | * @param[in] busp - Dbus obj | 
|  | 901 | * @param[in] service - service name | 
|  | 902 | * @param[in] obj - object path | 
|  | 903 | * | 
|  | 904 | * @return success completion code if it sets the session state to | 
|  | 905 | * tearDownInProgress else return the corresponding error completion code. | 
|  | 906 | **/ | 
|  | 907 | uint8_t setSessionState(std::shared_ptr<sdbusplus::asio::connection>& busp, | 
|  | 908 | const std::string& service, const std::string& obj) | 
|  | 909 | { | 
|  | 910 | try | 
|  | 911 | { | 
|  | 912 | uint8_t sessionState = std::get<uint8_t>(ipmi::getDbusProperty( | 
|  | 913 | *busp, service, obj, session::sessionIntf, "State")); | 
|  | 914 |  | 
|  | 915 | if (sessionState == static_cast<uint8_t>(session::State::active)) | 
|  | 916 | { | 
|  | 917 | ipmi::setDbusProperty( | 
|  | 918 | *busp, service, obj, session::sessionIntf, "State", | 
|  | 919 | static_cast<uint8_t>(session::State::tearDownInProgress)); | 
|  | 920 | return ipmi::ccSuccess; | 
|  | 921 | } | 
|  | 922 | } | 
| Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 923 | catch (const std::exception& e) | 
| Rajashekar Gade Reddy | e702392 | 2019-07-10 16:54:55 +0000 | [diff] [blame] | 924 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 925 | lg2::error("Failed in getting session state property, " | 
|  | 926 | "service: {SERVICE}, object path: {OBJECT_PATH}, " | 
|  | 927 | "interface: {INTERFACE}, error: {ERROR}", | 
|  | 928 | "SERVICE", service, "OBJECT_PATH", obj, "INTERFACE", | 
|  | 929 | session::sessionIntf, "ERROR", e); | 
| Rajashekar Gade Reddy | e702392 | 2019-07-10 16:54:55 +0000 | [diff] [blame] | 930 | return ipmi::ccUnspecifiedError; | 
|  | 931 | } | 
|  | 932 |  | 
|  | 933 | return ipmi::ccInvalidFieldRequest; | 
|  | 934 | } | 
|  | 935 |  | 
|  | 936 | ipmi::RspType<> ipmiAppCloseSession(uint32_t reqSessionId, | 
|  | 937 | std::optional<uint8_t> requestSessionHandle) | 
|  | 938 | { | 
|  | 939 | auto busp = getSdBus(); | 
|  | 940 | uint8_t reqSessionHandle = | 
|  | 941 | requestSessionHandle.value_or(session::defaultSessionHandle); | 
|  | 942 |  | 
|  | 943 | if (reqSessionId == session::sessionZero && | 
|  | 944 | reqSessionHandle == session::defaultSessionHandle) | 
|  | 945 | { | 
|  | 946 | return ipmi::response(session::ccInvalidSessionId); | 
|  | 947 | } | 
|  | 948 |  | 
|  | 949 | if (reqSessionId == session::sessionZero && | 
|  | 950 | reqSessionHandle == session::invalidSessionHandle) | 
|  | 951 | { | 
|  | 952 | return ipmi::response(session::ccInvalidSessionHandle); | 
|  | 953 | } | 
|  | 954 |  | 
|  | 955 | if (reqSessionId != session::sessionZero && | 
|  | 956 | reqSessionHandle != session::defaultSessionHandle) | 
|  | 957 | { | 
|  | 958 | return ipmi::response(ipmi::ccInvalidFieldRequest); | 
|  | 959 | } | 
|  | 960 |  | 
|  | 961 | try | 
|  | 962 | { | 
|  | 963 | ipmi::ObjectTree objectTree = ipmi::getAllDbusObjects( | 
|  | 964 | *busp, session::sessionManagerRootPath, session::sessionIntf); | 
|  | 965 |  | 
|  | 966 | for (auto& objectTreeItr : objectTree) | 
|  | 967 | { | 
|  | 968 | const std::string obj = objectTreeItr.first; | 
|  | 969 |  | 
|  | 970 | if (isSessionObjectMatched(obj, reqSessionId, reqSessionHandle)) | 
|  | 971 | { | 
|  | 972 | auto& serviceMap = objectTreeItr.second; | 
|  | 973 |  | 
|  | 974 | // Session id and session handle are unique for each session. | 
|  | 975 | // Session id and handler are retrived from the object path and | 
|  | 976 | // object path will be unique for each session. Checking if | 
|  | 977 | // multiple objects exist with same object path under multiple | 
|  | 978 | // services. | 
|  | 979 | if (serviceMap.size() != 1) | 
|  | 980 | { | 
|  | 981 | return ipmi::responseUnspecifiedError(); | 
|  | 982 | } | 
|  | 983 |  | 
|  | 984 | auto itr = serviceMap.begin(); | 
|  | 985 | const std::string service = itr->first; | 
|  | 986 | return ipmi::response(setSessionState(busp, service, obj)); | 
|  | 987 | } | 
|  | 988 | } | 
|  | 989 | } | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 990 | catch (const sdbusplus::exception_t& e) | 
| Rajashekar Gade Reddy | e702392 | 2019-07-10 16:54:55 +0000 | [diff] [blame] | 991 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 992 | lg2::error("Failed to fetch object from dbus, " | 
|  | 993 | "interface: {INTERFACE}, error: {ERROR}", | 
|  | 994 | "INTERFACE", session::sessionIntf, "ERROR", e); | 
| Rajashekar Gade Reddy | e702392 | 2019-07-10 16:54:55 +0000 | [diff] [blame] | 995 | return ipmi::responseUnspecifiedError(); | 
|  | 996 | } | 
|  | 997 |  | 
|  | 998 | return ipmi::responseInvalidFieldRequest(); | 
|  | 999 | } | 
|  | 1000 |  | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1001 | uint8_t getTotalSessionCount() | 
|  | 1002 | { | 
| Meera-Katta | c178948 | 2021-05-18 09:53:26 +0000 | [diff] [blame] | 1003 | uint8_t count = 0, ch = 0; | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1004 |  | 
|  | 1005 | while (ch < ipmi::maxIpmiChannels && | 
|  | 1006 | count < session::maxNetworkInstanceSupported) | 
|  | 1007 | { | 
| Jayaprakash Mutyala | 80207e6 | 2020-07-04 16:34:15 +0000 | [diff] [blame] | 1008 | ipmi::ChannelInfo chInfo{}; | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1009 | ipmi::getChannelInfo(ch, chInfo); | 
|  | 1010 | if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) == | 
|  | 1011 | ipmi::EChannelMediumType::lan8032) | 
|  | 1012 | { | 
|  | 1013 | count++; | 
|  | 1014 | } | 
|  | 1015 | ch++; | 
|  | 1016 | } | 
|  | 1017 | return count * session::maxSessionCountPerChannel; | 
|  | 1018 | } | 
|  | 1019 |  | 
|  | 1020 | /** | 
|  | 1021 | * @brief get session info request data. | 
|  | 1022 | * | 
|  | 1023 | * This function validates the request data and retrive request session id, | 
|  | 1024 | * session handle. | 
|  | 1025 | * | 
| Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 1026 | * @param[in] ctx - context of current session. | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1027 | * @param[in] sessionIndex - request session index | 
|  | 1028 | * @param[in] payload - input payload | 
|  | 1029 | * @param[in] reqSessionId - unpacked session Id will be asigned | 
|  | 1030 | * @param[in] reqSessionHandle - unpacked session handle will be asigned | 
|  | 1031 | * | 
|  | 1032 | * @return success completion code if request data is valid | 
|  | 1033 | * else return the correcponding error completion code. | 
|  | 1034 | **/ | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 1035 | uint8_t getSessionInfoRequestData( | 
|  | 1036 | const ipmi::Context::ptr ctx, const uint8_t sessionIndex, | 
|  | 1037 | ipmi::message::Payload& payload, uint32_t& reqSessionId, | 
|  | 1038 | uint8_t& reqSessionHandle) | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1039 | { | 
| Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 1040 | if ((sessionIndex > session::maxSessionCountPerChannel) && | 
|  | 1041 | (sessionIndex < session::searchSessionByHandle)) | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1042 | { | 
|  | 1043 | return ipmi::ccInvalidFieldRequest; | 
|  | 1044 | } | 
|  | 1045 |  | 
|  | 1046 | switch (sessionIndex) | 
|  | 1047 | { | 
| Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 1048 | case session::searchCurrentSession: | 
|  | 1049 |  | 
|  | 1050 | ipmi::ChannelInfo chInfo; | 
|  | 1051 | ipmi::getChannelInfo(ctx->channel, chInfo); | 
|  | 1052 |  | 
|  | 1053 | if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) != | 
|  | 1054 | ipmi::EChannelMediumType::lan8032) | 
|  | 1055 | { | 
|  | 1056 | return ipmi::ccInvalidFieldRequest; | 
|  | 1057 | } | 
|  | 1058 |  | 
|  | 1059 | if (!payload.fullyUnpacked()) | 
|  | 1060 | { | 
|  | 1061 | return ipmi::ccReqDataLenInvalid; | 
|  | 1062 | } | 
|  | 1063 | // Check if current sessionId is 0, sessionId 0 is reserved. | 
|  | 1064 | if (ctx->sessionId == session::sessionZero) | 
|  | 1065 | { | 
|  | 1066 | return session::ccInvalidSessionId; | 
|  | 1067 | } | 
|  | 1068 | reqSessionId = ctx->sessionId; | 
|  | 1069 | break; | 
|  | 1070 |  | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1071 | case session::searchSessionByHandle: | 
|  | 1072 |  | 
|  | 1073 | if ((payload.unpack(reqSessionHandle)) || | 
|  | 1074 | (!payload.fullyUnpacked())) | 
|  | 1075 | { | 
|  | 1076 | return ipmi::ccReqDataLenInvalid; | 
|  | 1077 | } | 
|  | 1078 |  | 
|  | 1079 | if ((reqSessionHandle == session::sessionZero) || | 
|  | 1080 | ((reqSessionHandle & session::multiIntfaceSessionHandleMask) > | 
|  | 1081 | session::maxSessionCountPerChannel)) | 
|  | 1082 | { | 
|  | 1083 | return session::ccInvalidSessionHandle; | 
|  | 1084 | } | 
|  | 1085 | break; | 
|  | 1086 |  | 
|  | 1087 | case session::searchSessionById: | 
|  | 1088 |  | 
|  | 1089 | if ((payload.unpack(reqSessionId)) || (!payload.fullyUnpacked())) | 
|  | 1090 | { | 
|  | 1091 | return ipmi::ccReqDataLenInvalid; | 
|  | 1092 | } | 
|  | 1093 |  | 
|  | 1094 | if (reqSessionId == session::sessionZero) | 
|  | 1095 | { | 
|  | 1096 | return session::ccInvalidSessionId; | 
|  | 1097 | } | 
|  | 1098 | break; | 
|  | 1099 |  | 
|  | 1100 | default: | 
|  | 1101 | if (!payload.fullyUnpacked()) | 
|  | 1102 | { | 
|  | 1103 | return ipmi::ccReqDataLenInvalid; | 
|  | 1104 | } | 
|  | 1105 | break; | 
|  | 1106 | } | 
|  | 1107 | return ipmi::ccSuccess; | 
|  | 1108 | } | 
|  | 1109 |  | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1110 | uint8_t getSessionState(ipmi::Context::ptr ctx, const std::string& service, | 
|  | 1111 | const std::string& objPath, uint8_t& sessionState) | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1112 | { | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1113 | boost::system::error_code ec = ipmi::getDbusProperty( | 
|  | 1114 | ctx, service, objPath, session::sessionIntf, "State", sessionState); | 
|  | 1115 | if (ec) | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1116 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1117 | lg2::error("Failed to fetch state property, service: {SERVICE}, " | 
|  | 1118 | "object path: {OBJECTPATH}, interface: {INTERFACE}, " | 
|  | 1119 | "error: {ERROR}", | 
|  | 1120 | "SERVICE", service, "OBJECTPATH", objPath, "INTERFACE", | 
|  | 1121 | session::sessionIntf, "ERROR", ec.message()); | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1122 | return ipmi::ccUnspecifiedError; | 
|  | 1123 | } | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1124 | return ipmi::ccSuccess; | 
|  | 1125 | } | 
|  | 1126 |  | 
|  | 1127 | static constexpr uint8_t macAddrLen = 6; | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1128 | /** Alias SessionDetails - contain the optional information about an | 
|  | 1129 | *        RMCP+ session. | 
|  | 1130 | * | 
|  | 1131 | *  @param userID - uint6_t session user ID (0-63) | 
|  | 1132 | *  @param reserved - uint2_t reserved | 
|  | 1133 | *  @param privilege - uint4_t session privilege (0-5) | 
|  | 1134 | *  @param reserved - uint4_t reserved | 
|  | 1135 | *  @param channel - uint4_t session channel number | 
|  | 1136 | *  @param protocol - uint4_t session protocol | 
|  | 1137 | *  @param remoteIP - uint32_t remote IP address | 
|  | 1138 | *  @param macAddr - std::array<uint8_t, 6> mac address | 
|  | 1139 | *  @param port - uint16_t remote port | 
|  | 1140 | */ | 
|  | 1141 | using SessionDetails = | 
|  | 1142 | std::tuple<uint2_t, uint6_t, uint4_t, uint4_t, uint4_t, uint4_t, uint32_t, | 
|  | 1143 | std::array<uint8_t, macAddrLen>, uint16_t>; | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1144 |  | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1145 | /** @brief get session details for a given session | 
|  | 1146 | * | 
|  | 1147 | *  @param[in] ctx - ipmi::Context pointer for accessing D-Bus | 
|  | 1148 | *  @param[in] service - D-Bus service name to fetch details from | 
|  | 1149 | *  @param[in] objPath - D-Bus object path for session | 
|  | 1150 | *  @param[out] sessionHandle - return session handle for session | 
|  | 1151 | *  @param[out] sessionState - return session state for session | 
|  | 1152 | *  @param[out] details - return a SessionDetails tuple containing other | 
|  | 1153 | *                        session info | 
|  | 1154 | *  @return - ipmi::Cc success or error code | 
|  | 1155 | */ | 
|  | 1156 | ipmi::Cc getSessionDetails(ipmi::Context::ptr ctx, const std::string& service, | 
|  | 1157 | const std::string& objPath, uint8_t& sessionHandle, | 
|  | 1158 | uint8_t& sessionState, SessionDetails& details) | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1159 | { | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1160 | ipmi::PropertyMap sessionProps; | 
|  | 1161 | boost::system::error_code ec = ipmi::getAllDbusProperties( | 
|  | 1162 | ctx, service, objPath, session::sessionIntf, sessionProps); | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1163 |  | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1164 | if (ec) | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1165 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1166 | lg2::error("Failed to fetch state property, service: {SERVICE}, " | 
|  | 1167 | "object path: {OBJECTPATH}, interface: {INTERFACE}, " | 
|  | 1168 | "error: {ERROR}", | 
|  | 1169 | "SERVICE", service, "OBJECTPATH", objPath, "INTERFACE", | 
|  | 1170 | session::sessionIntf, "ERROR", ec.message()); | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1171 | return ipmi::ccUnspecifiedError; | 
|  | 1172 | } | 
|  | 1173 |  | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1174 | sessionState = ipmi::mappedVariant<uint8_t>( | 
|  | 1175 | sessionProps, "State", static_cast<uint8_t>(session::State::inactive)); | 
|  | 1176 | if (sessionState == static_cast<uint8_t>(session::State::active)) | 
|  | 1177 | { | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 1178 | sessionHandle = | 
|  | 1179 | ipmi::mappedVariant<uint8_t>(sessionProps, "SessionHandle", 0); | 
|  | 1180 | std::get<0>(details) = | 
|  | 1181 | ipmi::mappedVariant<uint8_t>(sessionProps, "UserID", 0xff); | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1182 | // std::get<1>(details) = 0; // (default constructed to 0) | 
|  | 1183 | std::get<2>(details) = | 
|  | 1184 | ipmi::mappedVariant<uint8_t>(sessionProps, "CurrentPrivilege", 0); | 
|  | 1185 | // std::get<3>(details) = 0; // (default constructed to 0) | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 1186 | std::get<4>(details) = | 
|  | 1187 | ipmi::mappedVariant<uint8_t>(sessionProps, "ChannelNum", 0xff); | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1188 | constexpr uint4_t rmcpPlusProtocol = 1; | 
|  | 1189 | std::get<5>(details) = rmcpPlusProtocol; | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 1190 | std::get<6>(details) = | 
|  | 1191 | ipmi::mappedVariant<uint32_t>(sessionProps, "RemoteIPAddr", 0); | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1192 | // std::get<7>(details) = {{0}}; // default constructed to all 0 | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 1193 | std::get<8>(details) = | 
|  | 1194 | ipmi::mappedVariant<uint16_t>(sessionProps, "RemotePort", 0); | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1195 | } | 
|  | 1196 |  | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1197 | return ipmi::ccSuccess; | 
|  | 1198 | } | 
|  | 1199 |  | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1200 | ipmi::RspType<uint8_t, // session handle, | 
|  | 1201 | uint8_t, // total session count | 
|  | 1202 | uint8_t, // active session count | 
|  | 1203 | std::optional<SessionDetails>> | 
| Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 1204 | ipmiAppGetSessionInfo(ipmi::Context::ptr ctx, uint8_t sessionIndex, | 
|  | 1205 | ipmi::message::Payload& payload) | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1206 | { | 
|  | 1207 | uint32_t reqSessionId = 0; | 
|  | 1208 | uint8_t reqSessionHandle = session::defaultSessionHandle; | 
|  | 1209 | // initializing state to 0xff as 0 represents state as inactive. | 
|  | 1210 | uint8_t state = 0xFF; | 
|  | 1211 |  | 
|  | 1212 | uint8_t completionCode = getSessionInfoRequestData( | 
| Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 1213 | ctx, sessionIndex, payload, reqSessionId, reqSessionHandle); | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1214 |  | 
|  | 1215 | if (completionCode) | 
|  | 1216 | { | 
|  | 1217 | return ipmi::response(completionCode); | 
|  | 1218 | } | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1219 | ipmi::ObjectTree objectTree; | 
|  | 1220 | boost::system::error_code ec = ipmi::getAllDbusObjects( | 
|  | 1221 | ctx, session::sessionManagerRootPath, session::sessionIntf, objectTree); | 
|  | 1222 | if (ec) | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1223 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1224 | lg2::error("Failed to fetch object from dbus, " | 
|  | 1225 | "interface: {INTERFACE}, error: {ERROR}", | 
|  | 1226 | "INTERFACE", session::sessionIntf, "ERROR", ec.message()); | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1227 | return ipmi::responseUnspecifiedError(); | 
|  | 1228 | } | 
|  | 1229 |  | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1230 | uint8_t totalSessionCount = getTotalSessionCount(); | 
|  | 1231 | uint8_t activeSessionCount = 0; | 
|  | 1232 | uint8_t sessionHandle = session::defaultSessionHandle; | 
| Johnathan Mantey | ca93bb1 | 2022-12-02 13:33:17 -0800 | [diff] [blame] | 1233 | uint8_t activeSessionHandle = 0; | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1234 | std::optional<SessionDetails> maybeDetails; | 
|  | 1235 | uint8_t index = 0; | 
|  | 1236 | for (auto& objectTreeItr : objectTree) | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1237 | { | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1238 | uint32_t sessionId = 0; | 
|  | 1239 | std::string objectPath = objectTreeItr.first; | 
|  | 1240 |  | 
|  | 1241 | if (!parseCloseSessionInputPayload(objectPath, sessionId, | 
|  | 1242 | sessionHandle)) | 
|  | 1243 | { | 
|  | 1244 | continue; | 
|  | 1245 | } | 
|  | 1246 | index++; | 
|  | 1247 | auto& serviceMap = objectTreeItr.second; | 
|  | 1248 | auto itr = serviceMap.begin(); | 
|  | 1249 |  | 
|  | 1250 | if (serviceMap.size() != 1) | 
|  | 1251 | { | 
|  | 1252 | return ipmi::responseUnspecifiedError(); | 
|  | 1253 | } | 
|  | 1254 |  | 
|  | 1255 | std::string service = itr->first; | 
|  | 1256 | uint8_t sessionState = 0; | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 1257 | completionCode = | 
|  | 1258 | getSessionState(ctx, service, objectPath, sessionState); | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1259 | if (completionCode) | 
|  | 1260 | { | 
|  | 1261 | return ipmi::response(completionCode); | 
|  | 1262 | } | 
|  | 1263 |  | 
|  | 1264 | if (sessionState == static_cast<uint8_t>(session::State::active)) | 
|  | 1265 | { | 
|  | 1266 | activeSessionCount++; | 
|  | 1267 | } | 
|  | 1268 |  | 
| Johnathan Mantey | ca93bb1 | 2022-12-02 13:33:17 -0800 | [diff] [blame] | 1269 | if (index == sessionIndex || reqSessionId == sessionId || | 
|  | 1270 | reqSessionHandle == sessionHandle) | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1271 | { | 
| Johnathan Mantey | ca93bb1 | 2022-12-02 13:33:17 -0800 | [diff] [blame] | 1272 | SessionDetails details{}; | 
|  | 1273 | completionCode = getSessionDetails(ctx, service, objectPath, | 
|  | 1274 | sessionHandle, state, details); | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1275 |  | 
| Johnathan Mantey | ca93bb1 | 2022-12-02 13:33:17 -0800 | [diff] [blame] | 1276 | if (completionCode) | 
|  | 1277 | { | 
|  | 1278 | return ipmi::response(completionCode); | 
|  | 1279 | } | 
|  | 1280 | activeSessionHandle = sessionHandle; | 
|  | 1281 | maybeDetails = std::move(details); | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1282 | } | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1283 | } | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1284 |  | 
|  | 1285 | if (state == static_cast<uint8_t>(session::State::active) || | 
|  | 1286 | state == static_cast<uint8_t>(session::State::tearDownInProgress)) | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1287 | { | 
| Johnathan Mantey | ca93bb1 | 2022-12-02 13:33:17 -0800 | [diff] [blame] | 1288 | return ipmi::responseSuccess(activeSessionHandle, totalSessionCount, | 
| Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 1289 | activeSessionCount, maybeDetails); | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1290 | } | 
|  | 1291 |  | 
|  | 1292 | return ipmi::responseInvalidFieldRequest(); | 
|  | 1293 | } | 
|  | 1294 |  | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1295 | static std::unique_ptr<SysInfoParamStore> sysInfoParamStore; | 
|  | 1296 |  | 
| Xo Wang | 8765133 | 2017-08-11 10:17:59 -0700 | [diff] [blame] | 1297 | static std::string sysInfoReadSystemName() | 
|  | 1298 | { | 
|  | 1299 | // Use the BMC hostname as the "System Name." | 
|  | 1300 | char hostname[HOST_NAME_MAX + 1] = {}; | 
|  | 1301 | if (gethostname(hostname, HOST_NAME_MAX) != 0) | 
|  | 1302 | { | 
|  | 1303 | perror("System info parameter: system name"); | 
|  | 1304 | } | 
|  | 1305 | return hostname; | 
|  | 1306 | } | 
|  | 1307 |  | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1308 | static constexpr uint8_t paramRevision = 0x11; | 
|  | 1309 | static constexpr size_t configParameterLength = 16; | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1310 |  | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1311 | static constexpr size_t smallChunkSize = 14; | 
|  | 1312 | static constexpr size_t fullChunkSize = 16; | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1313 | static constexpr uint8_t progressMask = 0x3; | 
| krishnar4 | 1075283 | 2023-11-06 13:58:35 +0530 | [diff] [blame] | 1314 | static constexpr uint8_t maxValidEncodingData = 0x02; | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1315 |  | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1316 | static constexpr uint8_t setComplete = 0x0; | 
|  | 1317 | static constexpr uint8_t setInProgress = 0x1; | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1318 | static uint8_t transferStatus = setComplete; | 
|  | 1319 |  | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1320 | static constexpr uint8_t configDataOverhead = 2; | 
|  | 1321 |  | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1322 | namespace ipmi | 
|  | 1323 | { | 
|  | 1324 | constexpr Cc ccParmNotSupported = 0x80; | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1325 | constexpr Cc ccSetInProgressActive = 0x81; | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1326 |  | 
|  | 1327 | static inline auto responseParmNotSupported() | 
|  | 1328 | { | 
|  | 1329 | return response(ccParmNotSupported); | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1330 | } | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1331 | static inline auto responseSetInProgressActive() | 
|  | 1332 | { | 
|  | 1333 | return response(ccSetInProgressActive); | 
|  | 1334 | } | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1335 | } // namespace ipmi | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1336 |  | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1337 | ipmi::RspType<uint8_t,                // Parameter revision | 
|  | 1338 | std::optional<uint8_t>, // data1 / setSelector / ProgressStatus | 
|  | 1339 | std::optional<std::vector<uint8_t>>> // data2-17 | 
| jayaprakash Mutyala | c2566a9 | 2020-04-23 21:18:35 +0000 | [diff] [blame] | 1340 | ipmiAppGetSystemInfo(uint7_t reserved, bool getRevision, | 
|  | 1341 | uint8_t paramSelector, uint8_t setSelector, | 
|  | 1342 | uint8_t BlockSelector) | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1343 | { | 
| Snehalatha V | a5ae772 | 2020-05-02 18:18:57 +0000 | [diff] [blame] | 1344 | if (reserved || (paramSelector >= invalidParamSelectorStart && | 
|  | 1345 | paramSelector <= invalidParamSelectorEnd)) | 
| jayaprakash Mutyala | c2566a9 | 2020-04-23 21:18:35 +0000 | [diff] [blame] | 1346 | { | 
|  | 1347 | return ipmi::responseInvalidFieldRequest(); | 
|  | 1348 | } | 
| PavanKumarIntel | 3771f5f | 2023-11-02 06:26:42 +0000 | [diff] [blame] | 1349 | if (paramSelector >= oemCmdStart) | 
| Snehalatha V | a5ae772 | 2020-05-02 18:18:57 +0000 | [diff] [blame] | 1350 | { | 
|  | 1351 | return ipmi::responseParmNotSupported(); | 
|  | 1352 | } | 
| jayaprakash Mutyala | c2566a9 | 2020-04-23 21:18:35 +0000 | [diff] [blame] | 1353 | if (getRevision) | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1354 | { | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1355 | return ipmi::responseSuccess(paramRevision, std::nullopt, std::nullopt); | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1356 | } | 
|  | 1357 |  | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1358 | if (paramSelector == 0) | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1359 | { | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1360 | return ipmi::responseSuccess(paramRevision, transferStatus, | 
|  | 1361 | std::nullopt); | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1362 | } | 
|  | 1363 |  | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1364 | if (BlockSelector != 0) // 00h if parameter does not require a block number | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1365 | { | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1366 | return ipmi::responseParmNotSupported(); | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1367 | } | 
|  | 1368 |  | 
|  | 1369 | if (sysInfoParamStore == nullptr) | 
|  | 1370 | { | 
|  | 1371 | sysInfoParamStore = std::make_unique<SysInfoParamStore>(); | 
| Xo Wang | 8765133 | 2017-08-11 10:17:59 -0700 | [diff] [blame] | 1372 | sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME, | 
|  | 1373 | sysInfoReadSystemName); | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1374 | } | 
|  | 1375 |  | 
|  | 1376 | // Parameters other than Set In Progress are assumed to be strings. | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1377 | std::tuple<bool, std::string> ret = | 
|  | 1378 | sysInfoParamStore->lookup(paramSelector); | 
|  | 1379 | bool found = std::get<0>(ret); | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1380 | if (!found) | 
|  | 1381 | { | 
| Snehalatha V | a5ae772 | 2020-05-02 18:18:57 +0000 | [diff] [blame] | 1382 | return ipmi::responseSensorInvalid(); | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1383 | } | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1384 | std::string& paramString = std::get<1>(ret); | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1385 | std::vector<uint8_t> configData; | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1386 | size_t count = 0; | 
|  | 1387 | if (setSelector == 0) | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1388 | {                               // First chunk has only 14 bytes. | 
|  | 1389 | configData.emplace_back(0); // encoding | 
|  | 1390 | configData.emplace_back(paramString.length()); // string length | 
|  | 1391 | count = std::min(paramString.length(), smallChunkSize); | 
|  | 1392 | configData.resize(count + configDataOverhead); | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1393 | std::copy_n(paramString.begin(), count, | 
| Snehalatha V | a5ae772 | 2020-05-02 18:18:57 +0000 | [diff] [blame] | 1394 | configData.begin() + configDataOverhead); // 14 bytes chunk | 
|  | 1395 |  | 
|  | 1396 | // Append zero's to remaining bytes | 
|  | 1397 | if (configData.size() < configParameterLength) | 
|  | 1398 | { | 
|  | 1399 | std::fill_n(std::back_inserter(configData), | 
|  | 1400 | configParameterLength - configData.size(), 0x00); | 
|  | 1401 | } | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1402 | } | 
|  | 1403 | else | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1404 | { | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1405 | size_t offset = (setSelector * fullChunkSize) - configDataOverhead; | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1406 | if (offset >= paramString.length()) | 
|  | 1407 | { | 
|  | 1408 | return ipmi::responseParmOutOfRange(); | 
|  | 1409 | } | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1410 | count = std::min(paramString.length() - offset, fullChunkSize); | 
|  | 1411 | configData.resize(count); | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1412 | std::copy_n(paramString.begin() + offset, count, | 
|  | 1413 | configData.begin()); // 16 bytes chunk | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1414 | } | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1415 | return ipmi::responseSuccess(paramRevision, setSelector, configData); | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1416 | } | 
|  | 1417 |  | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1418 | ipmi::RspType<> ipmiAppSetSystemInfo(uint8_t paramSelector, uint8_t data1, | 
|  | 1419 | std::vector<uint8_t> configData) | 
|  | 1420 | { | 
| jayaprakash Mutyala | 3c5e413 | 2020-04-27 23:00:05 +0000 | [diff] [blame] | 1421 | if (paramSelector >= invalidParamSelectorStart && | 
|  | 1422 | paramSelector <= invalidParamSelectorEnd) | 
|  | 1423 | { | 
|  | 1424 | return ipmi::responseInvalidFieldRequest(); | 
|  | 1425 | } | 
| PavanKumarIntel | 3771f5f | 2023-11-02 06:26:42 +0000 | [diff] [blame] | 1426 | if (paramSelector >= oemCmdStart) | 
| jayaprakash Mutyala | 3c5e413 | 2020-04-27 23:00:05 +0000 | [diff] [blame] | 1427 | { | 
|  | 1428 | return ipmi::responseParmNotSupported(); | 
|  | 1429 | } | 
|  | 1430 |  | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1431 | if (paramSelector == 0) | 
|  | 1432 | { | 
|  | 1433 | // attempt to set the 'set in progress' value (in parameter #0) | 
|  | 1434 | // when not in the set complete state. | 
|  | 1435 | if ((transferStatus != setComplete) && (data1 == setInProgress)) | 
|  | 1436 | { | 
|  | 1437 | return ipmi::responseSetInProgressActive(); | 
|  | 1438 | } | 
|  | 1439 | // only following 2 states are supported | 
|  | 1440 | if (data1 > setInProgress) | 
|  | 1441 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1442 | lg2::error("illegal SetInProgress status"); | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1443 | return ipmi::responseInvalidFieldRequest(); | 
|  | 1444 | } | 
|  | 1445 |  | 
|  | 1446 | transferStatus = data1 & progressMask; | 
|  | 1447 | return ipmi::responseSuccess(); | 
|  | 1448 | } | 
|  | 1449 |  | 
|  | 1450 | if (configData.size() > configParameterLength) | 
|  | 1451 | { | 
|  | 1452 | return ipmi::responseInvalidFieldRequest(); | 
|  | 1453 | } | 
|  | 1454 |  | 
| jayaprakash Mutyala | 3c5e413 | 2020-04-27 23:00:05 +0000 | [diff] [blame] | 1455 | // Append zero's to remaining bytes | 
|  | 1456 | if (configData.size() < configParameterLength) | 
|  | 1457 | { | 
|  | 1458 | fill_n(back_inserter(configData), | 
|  | 1459 | (configParameterLength - configData.size()), 0x00); | 
|  | 1460 | } | 
|  | 1461 |  | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1462 | if (!sysInfoParamStore) | 
|  | 1463 | { | 
|  | 1464 | sysInfoParamStore = std::make_unique<SysInfoParamStore>(); | 
|  | 1465 | sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME, | 
|  | 1466 | sysInfoReadSystemName); | 
|  | 1467 | } | 
|  | 1468 |  | 
|  | 1469 | // lookup | 
|  | 1470 | std::tuple<bool, std::string> ret = | 
|  | 1471 | sysInfoParamStore->lookup(paramSelector); | 
|  | 1472 | bool found = std::get<0>(ret); | 
|  | 1473 | std::string& paramString = std::get<1>(ret); | 
|  | 1474 | if (!found) | 
|  | 1475 | { | 
|  | 1476 | // parameter does not exist. Init new | 
|  | 1477 | paramString = ""; | 
|  | 1478 | } | 
|  | 1479 |  | 
|  | 1480 | uint8_t setSelector = data1; | 
|  | 1481 | size_t count = 0; | 
| krishnar4 | 1075283 | 2023-11-06 13:58:35 +0530 | [diff] [blame] | 1482 | if (setSelector == 0) // First chunk has only 14 bytes. | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1483 | { | 
| krishnar4 | 1075283 | 2023-11-06 13:58:35 +0530 | [diff] [blame] | 1484 | uint8_t encoding = configData.at(0); | 
|  | 1485 | if (encoding > maxValidEncodingData) | 
|  | 1486 | { | 
|  | 1487 | return ipmi::responseInvalidFieldRequest(); | 
|  | 1488 | } | 
|  | 1489 |  | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1490 | size_t stringLen = configData.at(1); // string length | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1491 | count = std::min(stringLen, smallChunkSize); | 
|  | 1492 | count = std::min(count, configData.size()); | 
|  | 1493 | paramString.resize(stringLen); // reserve space | 
|  | 1494 | std::copy_n(configData.begin() + configDataOverhead, count, | 
|  | 1495 | paramString.begin()); | 
|  | 1496 | } | 
|  | 1497 | else | 
|  | 1498 | { | 
|  | 1499 | size_t offset = (setSelector * fullChunkSize) - configDataOverhead; | 
|  | 1500 | if (offset >= paramString.length()) | 
|  | 1501 | { | 
|  | 1502 | return ipmi::responseParmOutOfRange(); | 
|  | 1503 | } | 
|  | 1504 | count = std::min(paramString.length() - offset, configData.size()); | 
|  | 1505 | std::copy_n(configData.begin(), count, paramString.begin() + offset); | 
|  | 1506 | } | 
|  | 1507 | sysInfoParamStore->update(paramSelector, paramString); | 
|  | 1508 | return ipmi::responseSuccess(); | 
|  | 1509 | } | 
|  | 1510 |  | 
| Yong Li | bd0503a | 2019-08-22 17:17:17 +0800 | [diff] [blame] | 1511 | #ifdef ENABLE_I2C_WHITELIST_CHECK | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1512 | inline std::vector<uint8_t> convertStringToData(const std::string& command) | 
|  | 1513 | { | 
|  | 1514 | std::istringstream iss(command); | 
|  | 1515 | std::string token; | 
|  | 1516 | std::vector<uint8_t> dataValue; | 
|  | 1517 | while (std::getline(iss, token, ' ')) | 
|  | 1518 | { | 
|  | 1519 | dataValue.emplace_back( | 
|  | 1520 | static_cast<uint8_t>(std::stoul(token, nullptr, base_16))); | 
|  | 1521 | } | 
|  | 1522 | return dataValue; | 
|  | 1523 | } | 
|  | 1524 |  | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1525 | static bool populateI2CControllerWRAllowlist() | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1526 | { | 
|  | 1527 | nlohmann::json data = nullptr; | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1528 | std::ifstream jsonFile(i2cControllerWRAllowlistFile); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1529 |  | 
|  | 1530 | if (!jsonFile.good()) | 
|  | 1531 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1532 | lg2::warning("i2c allow list file not found! file name: {FILE_NAME}", | 
|  | 1533 | "FILE_NAME", i2cControllerWRAllowlistFile); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1534 | return false; | 
|  | 1535 | } | 
|  | 1536 |  | 
|  | 1537 | try | 
|  | 1538 | { | 
|  | 1539 | data = nlohmann::json::parse(jsonFile, nullptr, false); | 
|  | 1540 | } | 
| Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 1541 | catch (const nlohmann::json::parse_error& e) | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1542 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1543 | lg2::error("Corrupted i2c allow list config file, " | 
|  | 1544 | "file name: {FILE_NAME}, error: {ERROR}", | 
|  | 1545 | "FILE_NAME", i2cControllerWRAllowlistFile, "ERROR", e); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1546 | return false; | 
|  | 1547 | } | 
|  | 1548 |  | 
|  | 1549 | try | 
|  | 1550 | { | 
|  | 1551 | // Example JSON Structure format | 
|  | 1552 | // "filters": [ | 
|  | 1553 | //    { | 
|  | 1554 | //      "Description": "Allow full read - ignore first byte write value | 
|  | 1555 | //      for 0x40 to 0x4F", | 
|  | 1556 | //      "busId": "0x01", | 
|  | 1557 | //      "slaveAddr": "0x40", | 
|  | 1558 | //      "slaveAddrMask": "0x0F", | 
|  | 1559 | //      "command": "0x00", | 
|  | 1560 | //      "commandMask": "0xFF" | 
|  | 1561 | //    }, | 
|  | 1562 | //    { | 
|  | 1563 | //      "Description": "Allow full read - first byte match 0x05 and | 
|  | 1564 | //      ignore second byte", | 
|  | 1565 | //      "busId": "0x01", | 
|  | 1566 | //      "slaveAddr": "0x57", | 
|  | 1567 | //      "slaveAddrMask": "0x00", | 
|  | 1568 | //      "command": "0x05 0x00", | 
|  | 1569 | //      "commandMask": "0x00 0xFF" | 
|  | 1570 | //    },] | 
|  | 1571 |  | 
|  | 1572 | nlohmann::json filters = data[filtersStr].get<nlohmann::json>(); | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1573 | std::vector<i2cControllerWRAllowlist>& allowlist = getWRAllowlist(); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1574 | for (const auto& it : filters.items()) | 
|  | 1575 | { | 
|  | 1576 | nlohmann::json filter = it.value(); | 
|  | 1577 | if (filter.is_null()) | 
|  | 1578 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1579 | lg2::error( | 
|  | 1580 | "Corrupted I2C controller write read allowlist config file, " | 
|  | 1581 | "file name: {FILE_NAME}", | 
|  | 1582 | "FILE_NAME", i2cControllerWRAllowlistFile); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1583 | return false; | 
|  | 1584 | } | 
|  | 1585 | const std::vector<uint8_t>& writeData = | 
|  | 1586 | convertStringToData(filter[cmdStr].get<std::string>()); | 
|  | 1587 | const std::vector<uint8_t>& writeDataMask = | 
|  | 1588 | convertStringToData(filter[cmdMaskStr].get<std::string>()); | 
|  | 1589 | if (writeDataMask.size() != writeData.size()) | 
|  | 1590 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1591 | lg2::error("I2C controller write read allowlist filter " | 
|  | 1592 | "mismatch for command & mask size"); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1593 | return false; | 
|  | 1594 | } | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1595 | allowlist.push_back( | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1596 | {static_cast<uint8_t>(std::stoul( | 
|  | 1597 | filter[busIdStr].get<std::string>(), nullptr, base_16)), | 
|  | 1598 | static_cast<uint8_t>( | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1599 | std::stoul(filter[targetAddrStr].get<std::string>(), | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1600 | nullptr, base_16)), | 
|  | 1601 | static_cast<uint8_t>( | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1602 | std::stoul(filter[targetAddrMaskStr].get<std::string>(), | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1603 | nullptr, base_16)), | 
|  | 1604 | writeData, writeDataMask}); | 
|  | 1605 | } | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1606 | if (allowlist.size() != filters.size()) | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1607 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1608 | lg2::error( | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1609 | "I2C controller write read allowlist filter size mismatch"); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1610 | return false; | 
|  | 1611 | } | 
|  | 1612 | } | 
| Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 1613 | catch (const std::exception& e) | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1614 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1615 | lg2::error("I2C controller write read allowlist " | 
|  | 1616 | "unexpected exception: {ERROR}", | 
|  | 1617 | "ERROR", e); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1618 | return false; | 
|  | 1619 | } | 
|  | 1620 | return true; | 
|  | 1621 | } | 
|  | 1622 |  | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1623 | static inline bool isWriteDataAllowlisted(const std::vector<uint8_t>& data, | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1624 | const std::vector<uint8_t>& dataMask, | 
|  | 1625 | const std::vector<uint8_t>& writeData) | 
|  | 1626 | { | 
|  | 1627 | std::vector<uint8_t> processedDataBuf(data.size()); | 
|  | 1628 | std::vector<uint8_t> processedReqBuf(dataMask.size()); | 
|  | 1629 | std::transform(writeData.begin(), writeData.end(), dataMask.begin(), | 
|  | 1630 | processedReqBuf.begin(), std::bit_or<uint8_t>()); | 
|  | 1631 | std::transform(data.begin(), data.end(), dataMask.begin(), | 
|  | 1632 | processedDataBuf.begin(), std::bit_or<uint8_t>()); | 
|  | 1633 |  | 
|  | 1634 | return (processedDataBuf == processedReqBuf); | 
|  | 1635 | } | 
|  | 1636 |  | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1637 | static bool isCmdAllowlisted(uint8_t busId, uint8_t targetAddr, | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1638 | std::vector<uint8_t>& writeData) | 
|  | 1639 | { | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1640 | std::vector<i2cControllerWRAllowlist>& allowList = getWRAllowlist(); | 
|  | 1641 | for (const auto& wlEntry : allowList) | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1642 | { | 
|  | 1643 | if ((busId == wlEntry.busId) && | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1644 | ((targetAddr | wlEntry.targetAddrMask) == | 
|  | 1645 | (wlEntry.targetAddr | wlEntry.targetAddrMask))) | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1646 | { | 
|  | 1647 | const std::vector<uint8_t>& dataMask = wlEntry.dataMask; | 
|  | 1648 | // Skip as no-match, if requested write data is more than the | 
|  | 1649 | // write data mask size | 
|  | 1650 | if (writeData.size() > dataMask.size()) | 
|  | 1651 | { | 
|  | 1652 | continue; | 
|  | 1653 | } | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1654 | if (isWriteDataAllowlisted(wlEntry.data, dataMask, writeData)) | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1655 | { | 
|  | 1656 | return true; | 
|  | 1657 | } | 
|  | 1658 | } | 
|  | 1659 | } | 
|  | 1660 | return false; | 
|  | 1661 | } | 
| Yong Li | bd0503a | 2019-08-22 17:17:17 +0800 | [diff] [blame] | 1662 | #else | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1663 | static bool populateI2CControllerWRAllowlist() | 
| Yong Li | bd0503a | 2019-08-22 17:17:17 +0800 | [diff] [blame] | 1664 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1665 | lg2::info("I2C_WHITELIST_CHECK is disabled, do not populate allowlist"); | 
| Yong Li | bd0503a | 2019-08-22 17:17:17 +0800 | [diff] [blame] | 1666 | return true; | 
|  | 1667 | } | 
|  | 1668 | #endif // ENABLE_I2C_WHITELIST_CHECK | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1669 |  | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1670 | /** @brief implements controller write read IPMI command which can be used for | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1671 | * low-level I2C/SMBus write, read or write-read access | 
|  | 1672 | *  @param isPrivateBus -to indicate private bus usage | 
|  | 1673 | *  @param busId - bus id | 
|  | 1674 | *  @param channelNum - channel number | 
|  | 1675 | *  @param reserved - skip 1 bit | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1676 | *  @param targetAddr - target address | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1677 | *  @param read count - number of bytes to be read | 
|  | 1678 | *  @param writeData - data to be written | 
|  | 1679 | * | 
|  | 1680 | *  @returns IPMI completion code plus response data | 
|  | 1681 | *   - readData - i2c response data | 
|  | 1682 | */ | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 1683 | ipmi::RspType<std::vector<uint8_t>> ipmiControllerWriteRead( | 
|  | 1684 | [[maybe_unused]] bool isPrivateBus, uint3_t busId, | 
|  | 1685 | [[maybe_unused]] uint4_t channelNum, bool reserved, uint7_t targetAddr, | 
|  | 1686 | uint8_t readCount, std::vector<uint8_t> writeData) | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1687 | { | 
| Jayaprakash Mutyala | c2af98b | 2021-09-14 09:19:11 +0000 | [diff] [blame] | 1688 | if (reserved) | 
|  | 1689 | { | 
|  | 1690 | return ipmi::responseInvalidFieldRequest(); | 
|  | 1691 | } | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1692 | const size_t writeCount = writeData.size(); | 
|  | 1693 | if (!readCount && !writeCount) | 
|  | 1694 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1695 | lg2::error("Controller write read command: Read & write count are 0"); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1696 | return ipmi::responseInvalidFieldRequest(); | 
|  | 1697 | } | 
| Yong Li | bd0503a | 2019-08-22 17:17:17 +0800 | [diff] [blame] | 1698 | #ifdef ENABLE_I2C_WHITELIST_CHECK | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1699 | if (!isCmdAllowlisted(static_cast<uint8_t>(busId), | 
|  | 1700 | static_cast<uint8_t>(targetAddr), writeData)) | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1701 | { | 
| George Liu | 24fffdc | 2024-07-17 17:40:53 +0800 | [diff] [blame] | 1702 | lg2::error("Controller write read request blocked!, " | 
|  | 1703 | "bus: {BUS}, addr: {ADDR}", | 
|  | 1704 | "BUS", static_cast<uint8_t>(busId), "ADDR", lg2::hex, | 
|  | 1705 | static_cast<uint8_t>(targetAddr)); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1706 | } | 
| Yong Li | bd0503a | 2019-08-22 17:17:17 +0800 | [diff] [blame] | 1707 | #endif // ENABLE_I2C_WHITELIST_CHECK | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1708 | std::vector<uint8_t> readBuf(readCount); | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 1709 | std::string i2cBus = | 
|  | 1710 | "/dev/i2c-" + std::to_string(static_cast<uint8_t>(busId)); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1711 |  | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1712 | ipmi::Cc ret = ipmi::i2cWriteRead(i2cBus, static_cast<uint8_t>(targetAddr), | 
| Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 1713 | writeData, readBuf); | 
|  | 1714 | if (ret != ipmi::ccSuccess) | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1715 | { | 
| Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 1716 | return ipmi::response(ret); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1717 | } | 
|  | 1718 | return ipmi::responseSuccess(readBuf); | 
|  | 1719 | } | 
|  | 1720 |  | 
| Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 1721 | void register_netfn_app_functions() | 
| vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 1722 | { | 
| Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 1723 | // <Get Device ID> | 
|  | 1724 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1725 | ipmi::app::cmdGetDeviceId, ipmi::Privilege::User, | 
|  | 1726 | ipmiAppGetDeviceId); | 
|  | 1727 |  | 
| Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1728 | // <Get BT Interface Capabilities> | 
| Vernon Mauery | cc99ba4 | 2019-03-25 13:40:11 -0700 | [diff] [blame] | 1729 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1730 | ipmi::app::cmdGetBtIfaceCapabilities, | 
|  | 1731 | ipmi::Privilege::User, ipmiAppGetBtCapabilities); | 
| Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 1732 |  | 
| Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1733 | // <Reset Watchdog Timer> | 
| Vernon Mauery | 11df4f6 | 2019-03-25 14:17:54 -0700 | [diff] [blame] | 1734 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1735 | ipmi::app::cmdResetWatchdogTimer, | 
|  | 1736 | ipmi::Privilege::Operator, ipmiAppResetWatchdogTimer); | 
| Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 1737 |  | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1738 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
| AppaRao Puli | 5a98ea6 | 2019-11-10 21:15:02 +0530 | [diff] [blame] | 1739 | ipmi::app::cmdGetSessionInfo, ipmi::Privilege::User, | 
|  | 1740 | ipmiAppGetSessionInfo); | 
| Rajashekar Gade Reddy | f71444d | 2019-07-25 15:12:17 +0000 | [diff] [blame] | 1741 |  | 
| Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1742 | // <Set Watchdog Timer> | 
| Deepak Kumar Sahu | cfae948 | 2019-05-20 14:58:58 +0000 | [diff] [blame] | 1743 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1744 | ipmi::app::cmdSetWatchdogTimer, | 
|  | 1745 | ipmi::Privilege::Operator, ipmiSetWatchdogTimer); | 
| Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 1746 |  | 
| Rajashekar Gade Reddy | e702392 | 2019-07-10 16:54:55 +0000 | [diff] [blame] | 1747 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1748 | ipmi::app::cmdCloseSession, ipmi::Privilege::Callback, | 
|  | 1749 | ipmiAppCloseSession); | 
|  | 1750 |  | 
| William A. Kennington III | 73f4451 | 2018-02-09 15:28:46 -0800 | [diff] [blame] | 1751 | // <Get Watchdog Timer> | 
| Deepak Kumar Sahu | cfae948 | 2019-05-20 14:58:58 +0000 | [diff] [blame] | 1752 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
| AppaRao Puli | 5a98ea6 | 2019-11-10 21:15:02 +0530 | [diff] [blame] | 1753 | ipmi::app::cmdGetWatchdogTimer, ipmi::Privilege::User, | 
|  | 1754 | ipmiGetWatchdogTimer); | 
| William A. Kennington III | 73f4451 | 2018-02-09 15:28:46 -0800 | [diff] [blame] | 1755 |  | 
| Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1756 | // <Get Self Test Results> | 
| Vernon Mauery | b84a528 | 2019-03-25 13:39:03 -0700 | [diff] [blame] | 1757 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1758 | ipmi::app::cmdGetSelfTestResults, | 
|  | 1759 | ipmi::Privilege::User, ipmiAppGetSelfTestResults); | 
| Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 1760 |  | 
| Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1761 | // <Get Device GUID> | 
| Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 1762 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1763 | ipmi::app::cmdGetDeviceGuid, ipmi::Privilege::User, | 
|  | 1764 | ipmiAppGetDeviceGuid); | 
| Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 1765 |  | 
| Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1766 | // <Set ACPI Power State> | 
| Deepak Kumar Sahu | 520c131 | 2019-05-17 18:14:09 +0000 | [diff] [blame] | 1767 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1768 | ipmi::app::cmdSetAcpiPowerState, | 
|  | 1769 | ipmi::Privilege::Admin, ipmiSetAcpiPowerState); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 1770 | // <Get ACPI Power State> | 
| Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 1771 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1772 | ipmi::app::cmdGetAcpiPowerState, | 
| AppaRao Puli | 5a98ea6 | 2019-11-10 21:15:02 +0530 | [diff] [blame] | 1773 | ipmi::Privilege::User, ipmiGetAcpiPowerState); | 
| Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 1774 |  | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1775 | // Note: For security reason, this command will be registered only when | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1776 | // there are proper I2C Controller write read allowlist | 
|  | 1777 | if (populateI2CControllerWRAllowlist()) | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1778 | { | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1779 | // Note: For security reasons, registering controller write read as | 
|  | 1780 | // admin privilege command, even though IPMI 2.0 specification allows it | 
|  | 1781 | // as operator privilege. | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1782 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1783 | ipmi::app::cmdMasterWriteRead, | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 1784 | ipmi::Privilege::Admin, ipmiControllerWriteRead); | 
| Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1785 | } | 
|  | 1786 |  | 
| Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 1787 | // <Get System GUID Command> | 
| Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 1788 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1789 | ipmi::app::cmdGetSystemGuid, ipmi::Privilege::User, | 
|  | 1790 | ipmiAppGetSystemGuid); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 1791 |  | 
|  | 1792 | // <Get Channel Cipher Suites Command> | 
| Ayushi Smriti | 5c3b72c | 2019-08-30 13:47:31 +0000 | [diff] [blame] | 1793 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1794 | ipmi::app::cmdGetChannelCipherSuites, | 
| Vernon Mauery | 79b4eea | 2019-11-07 09:51:39 -0800 | [diff] [blame] | 1795 | ipmi::Privilege::None, getChannelCipherSuites); | 
| Vernon Mauery | d2a57de | 2019-03-25 12:45:28 -0700 | [diff] [blame] | 1796 |  | 
| Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1797 | // <Get System Info Command> | 
| Jia, chunhui | 98b43ba | 2019-09-05 15:54:23 +0800 | [diff] [blame] | 1798 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1799 | ipmi::app::cmdGetSystemInfoParameters, | 
|  | 1800 | ipmi::Privilege::User, ipmiAppGetSystemInfo); | 
| Jia, chunhui | 449f216 | 2019-09-11 16:51:51 +0800 | [diff] [blame] | 1801 | // <Set System Info Command> | 
|  | 1802 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, | 
|  | 1803 | ipmi::app::cmdSetSystemInfoParameters, | 
|  | 1804 | ipmi::Privilege::Admin, ipmiAppSetSystemInfo); | 
| vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 1805 | return; | 
|  | 1806 | } |