Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1 | #include <arpa/inet.h> |
Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 2 | #include <fcntl.h> |
Xo Wang | 8765133 | 2017-08-11 10:17:59 -0700 | [diff] [blame] | 3 | #include <limits.h> |
Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 4 | #include <linux/i2c-dev.h> |
| 5 | #include <linux/i2c.h> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 6 | #include <mapper.h> |
Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 7 | #include <sys/ioctl.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <sys/types.h> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 10 | #include <systemd/sd-bus.h> |
Xo Wang | 8765133 | 2017-08-11 10:17:59 -0700 | [diff] [blame] | 11 | #include <unistd.h> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 12 | |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 13 | #include <algorithm> |
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 Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 17 | #include <array> |
| 18 | #include <cstddef> |
Vernon Mauery | 0120b68 | 2019-03-25 13:08:54 -0700 | [diff] [blame] | 19 | #include <cstdint> |
Vernon Mauery | bdda800 | 2019-02-26 10:18:51 -0800 | [diff] [blame] | 20 | #include <filesystem> |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 21 | #include <fstream> |
Vernon Mauery | 0120b68 | 2019-03-25 13:08:54 -0700 | [diff] [blame] | 22 | #include <ipmid/api.hpp> |
Vernon Mauery | 3325024 | 2019-03-12 16:49:26 -0700 | [diff] [blame] | 23 | #include <ipmid/types.hpp> |
Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 24 | #include <ipmid/utils.hpp> |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 25 | #include <memory> |
Patrick Venture | 46470a3 | 2018-09-07 19:26:25 -0700 | [diff] [blame] | 26 | #include <nlohmann/json.hpp> |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 27 | #include <phosphor-logging/elog-errors.hpp> |
| 28 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 29 | #include <sdbusplus/message/types.hpp> |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 30 | #include <string> |
Vernon Mauery | 0120b68 | 2019-03-25 13:08:54 -0700 | [diff] [blame] | 31 | #include <sys_info_param.hpp> |
| 32 | #include <transporthandler.hpp> |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 33 | #include <tuple> |
| 34 | #include <vector> |
| 35 | #include <xyz/openbmc_project/Common/error.hpp> |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 36 | #include <xyz/openbmc_project/Control/Power/ACPIPowerState/server.hpp> |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 37 | #include <xyz/openbmc_project/Software/Activation/server.hpp> |
| 38 | #include <xyz/openbmc_project/Software/Version/server.hpp> |
| 39 | #include <xyz/openbmc_project/State/BMC/server.hpp> |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 40 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 41 | extern sd_bus* bus; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 42 | |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 43 | constexpr auto bmc_state_interface = "xyz.openbmc_project.State.BMC"; |
| 44 | constexpr auto bmc_state_property = "CurrentBMCState"; |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 45 | |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 46 | static constexpr auto redundancyIntf = |
| 47 | "xyz.openbmc_project.Software.RedundancyPriority"; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 48 | static constexpr auto versionIntf = "xyz.openbmc_project.Software.Version"; |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 49 | static constexpr auto activationIntf = |
| 50 | "xyz.openbmc_project.Software.Activation"; |
| 51 | static constexpr auto softwareRoot = "/xyz/openbmc_project/software"; |
| 52 | |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 53 | void register_netfn_app_functions() __attribute__((constructor)); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 54 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 55 | using namespace phosphor::logging; |
| 56 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 57 | using Version = sdbusplus::xyz::openbmc_project::Software::server::Version; |
| 58 | using Activation = |
| 59 | sdbusplus::xyz::openbmc_project::Software::server::Activation; |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 60 | using BMC = sdbusplus::xyz::openbmc_project::State::server::BMC; |
Vernon Mauery | 185b9f8 | 2018-07-20 10:52:36 -0700 | [diff] [blame] | 61 | namespace fs = std::filesystem; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 62 | |
Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 63 | typedef struct |
| 64 | { |
| 65 | uint8_t busId; |
| 66 | uint8_t slaveAddr; |
| 67 | uint8_t slaveAddrMask; |
| 68 | std::vector<uint8_t> data; |
| 69 | std::vector<uint8_t> dataMask; |
| 70 | } i2cMasterWRWhitelist; |
| 71 | |
| 72 | static std::vector<i2cMasterWRWhitelist>& getWRWhitelist() |
| 73 | { |
| 74 | static std::vector<i2cMasterWRWhitelist> wrWhitelist; |
| 75 | return wrWhitelist; |
| 76 | } |
| 77 | |
| 78 | static constexpr const char* i2cMasterWRWhitelistFile = |
| 79 | "/usr/share/ipmi-providers/master_write_read_white_list.json"; |
| 80 | |
| 81 | static constexpr uint8_t maxIPMIWriteReadSize = 144; |
| 82 | static constexpr const char* filtersStr = "filters"; |
| 83 | static constexpr const char* busIdStr = "busId"; |
| 84 | static constexpr const char* slaveAddrStr = "slaveAddr"; |
| 85 | static constexpr const char* slaveAddrMaskStr = "slaveAddrMask"; |
| 86 | static constexpr const char* cmdStr = "command"; |
| 87 | static constexpr const char* cmdMaskStr = "commandMask"; |
| 88 | static constexpr int base_16 = 16; |
| 89 | |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 90 | /** |
| 91 | * @brief Returns the Version info from primary s/w object |
| 92 | * |
| 93 | * Get the Version info from the active s/w object which is having high |
| 94 | * "Priority" value(a smaller number is a higher priority) and "Purpose" |
| 95 | * is "BMC" from the list of all s/w objects those are implementing |
| 96 | * RedundancyPriority interface from the given softwareRoot path. |
| 97 | * |
| 98 | * @return On success returns the Version info from primary s/w object. |
| 99 | * |
| 100 | */ |
Vernon Mauery | ea1c401 | 2019-05-24 13:26:16 -0700 | [diff] [blame] | 101 | std::string getActiveSoftwareVersionInfo(ipmi::Context::ptr ctx) |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 102 | { |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 103 | std::string revision{}; |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 104 | ipmi::ObjectTree objectTree; |
| 105 | try |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 106 | { |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 107 | objectTree = |
Vernon Mauery | ea1c401 | 2019-05-24 13:26:16 -0700 | [diff] [blame] | 108 | ipmi::getAllDbusObjects(*ctx->bus, softwareRoot, redundancyIntf); |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 109 | } |
| 110 | catch (sdbusplus::exception::SdBusError& e) |
| 111 | { |
| 112 | log<level::ERR>("Failed to fetch redundancy object from dbus", |
| 113 | entry("INTERFACE=%s", redundancyIntf), |
| 114 | entry("ERRMSG=%s", e.what())); |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 115 | elog<InternalFailure>(); |
| 116 | } |
| 117 | |
| 118 | auto objectFound = false; |
| 119 | for (auto& softObject : objectTree) |
| 120 | { |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 121 | auto service = |
Vernon Mauery | ea1c401 | 2019-05-24 13:26:16 -0700 | [diff] [blame] | 122 | ipmi::getService(*ctx->bus, redundancyIntf, softObject.first); |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 123 | auto objValueTree = |
Vernon Mauery | ea1c401 | 2019-05-24 13:26:16 -0700 | [diff] [blame] | 124 | ipmi::getManagedObjects(*ctx->bus, service, softwareRoot); |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 125 | |
| 126 | auto minPriority = 0xFF; |
| 127 | for (const auto& objIter : objValueTree) |
| 128 | { |
| 129 | try |
| 130 | { |
| 131 | auto& intfMap = objIter.second; |
| 132 | auto& redundancyPriorityProps = intfMap.at(redundancyIntf); |
| 133 | auto& versionProps = intfMap.at(versionIntf); |
| 134 | auto& activationProps = intfMap.at(activationIntf); |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 135 | auto priority = |
| 136 | std::get<uint8_t>(redundancyPriorityProps.at("Priority")); |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 137 | auto purpose = |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 138 | std::get<std::string>(versionProps.at("Purpose")); |
| 139 | auto activation = |
| 140 | std::get<std::string>(activationProps.at("Activation")); |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 141 | auto version = |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 142 | std::get<std::string>(versionProps.at("Version")); |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 143 | if ((Version::convertVersionPurposeFromString(purpose) == |
| 144 | Version::VersionPurpose::BMC) && |
| 145 | (Activation::convertActivationsFromString(activation) == |
| 146 | Activation::Activations::Active)) |
| 147 | { |
| 148 | if (priority < minPriority) |
| 149 | { |
| 150 | minPriority = priority; |
| 151 | objectFound = true; |
| 152 | revision = std::move(version); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | catch (const std::exception& e) |
| 157 | { |
| 158 | log<level::ERR>(e.what()); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (!objectFound) |
| 164 | { |
| 165 | log<level::ERR>("Could not found an BMC software Object"); |
| 166 | elog<InternalFailure>(); |
| 167 | } |
| 168 | |
| 169 | return revision; |
| 170 | } |
| 171 | |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 172 | bool getCurrentBmcState() |
| 173 | { |
| 174 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 175 | |
| 176 | // Get the Inventory object implementing the BMC interface |
| 177 | ipmi::DbusObjectInfo bmcObject = |
| 178 | ipmi::getDbusObject(bus, bmc_state_interface); |
| 179 | auto variant = |
| 180 | ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first, |
| 181 | bmc_state_interface, bmc_state_property); |
| 182 | |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 183 | return std::holds_alternative<std::string>(variant) && |
| 184 | BMC::convertBMCStateFromString(std::get<std::string>(variant)) == |
| 185 | BMC::BMCState::Ready; |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 186 | } |
| 187 | |
Patrick Venture | 94930a1 | 2019-04-30 10:01:58 -0700 | [diff] [blame] | 188 | bool getCurrentBmcStateWithFallback(const bool fallbackAvailability) |
| 189 | { |
| 190 | try |
| 191 | { |
| 192 | return getCurrentBmcState(); |
| 193 | } |
| 194 | catch (...) |
| 195 | { |
| 196 | // Nothing provided the BMC interface, therefore return whatever was |
| 197 | // configured as the default. |
| 198 | return fallbackAvailability; |
| 199 | } |
| 200 | } |
| 201 | |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 202 | namespace acpi_state |
| 203 | { |
| 204 | using namespace sdbusplus::xyz::openbmc_project::Control::Power::server; |
| 205 | |
| 206 | const static constexpr char* acpiObjPath = |
| 207 | "/xyz/openbmc_project/control/host0/acpi_power_state"; |
| 208 | const static constexpr char* acpiInterface = |
| 209 | "xyz.openbmc_project.Control.Power.ACPIPowerState"; |
| 210 | const static constexpr char* sysACPIProp = "SysACPIStatus"; |
| 211 | const static constexpr char* devACPIProp = "DevACPIStatus"; |
| 212 | |
| 213 | enum class PowerStateType : uint8_t |
| 214 | { |
| 215 | sysPowerState = 0x00, |
| 216 | devPowerState = 0x01, |
| 217 | }; |
| 218 | |
| 219 | // Defined in 20.6 of ipmi doc |
| 220 | enum class PowerState : uint8_t |
| 221 | { |
| 222 | s0G0D0 = 0x00, |
| 223 | s1D1 = 0x01, |
| 224 | s2D2 = 0x02, |
| 225 | s3D3 = 0x03, |
| 226 | s4 = 0x04, |
| 227 | s5G2 = 0x05, |
| 228 | s4S5 = 0x06, |
| 229 | g3 = 0x07, |
| 230 | sleep = 0x08, |
| 231 | g1Sleep = 0x09, |
| 232 | override = 0x0a, |
| 233 | legacyOn = 0x20, |
| 234 | legacyOff = 0x21, |
| 235 | unknown = 0x2a, |
| 236 | noChange = 0x7f, |
| 237 | }; |
| 238 | |
| 239 | static constexpr uint8_t stateChanged = 0x80; |
| 240 | |
| 241 | struct ACPIState |
| 242 | { |
| 243 | uint8_t sysACPIState; |
| 244 | uint8_t devACPIState; |
| 245 | } __attribute__((packed)); |
| 246 | |
| 247 | std::map<ACPIPowerState::ACPI, PowerState> dbusToIPMI = { |
| 248 | {ACPIPowerState::ACPI::S0_G0_D0, PowerState::s0G0D0}, |
| 249 | {ACPIPowerState::ACPI::S1_D1, PowerState::s1D1}, |
| 250 | {ACPIPowerState::ACPI::S2_D2, PowerState::s2D2}, |
| 251 | {ACPIPowerState::ACPI::S3_D3, PowerState::s3D3}, |
| 252 | {ACPIPowerState::ACPI::S4, PowerState::s4}, |
| 253 | {ACPIPowerState::ACPI::S5_G2, PowerState::s5G2}, |
| 254 | {ACPIPowerState::ACPI::S4_S5, PowerState::s4S5}, |
| 255 | {ACPIPowerState::ACPI::G3, PowerState::g3}, |
| 256 | {ACPIPowerState::ACPI::SLEEP, PowerState::sleep}, |
| 257 | {ACPIPowerState::ACPI::G1_SLEEP, PowerState::g1Sleep}, |
| 258 | {ACPIPowerState::ACPI::OVERRIDE, PowerState::override}, |
| 259 | {ACPIPowerState::ACPI::LEGACY_ON, PowerState::legacyOn}, |
| 260 | {ACPIPowerState::ACPI::LEGACY_OFF, PowerState::legacyOff}, |
| 261 | {ACPIPowerState::ACPI::Unknown, PowerState::unknown}}; |
| 262 | |
| 263 | bool isValidACPIState(acpi_state::PowerStateType type, uint8_t state) |
| 264 | { |
| 265 | if (type == acpi_state::PowerStateType::sysPowerState) |
| 266 | { |
| 267 | if ((state <= static_cast<uint8_t>(acpi_state::PowerState::override)) || |
| 268 | (state == static_cast<uint8_t>(acpi_state::PowerState::legacyOn)) || |
| 269 | (state == |
| 270 | static_cast<uint8_t>(acpi_state::PowerState::legacyOff)) || |
| 271 | (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) || |
| 272 | (state == static_cast<uint8_t>(acpi_state::PowerState::noChange))) |
| 273 | { |
| 274 | return true; |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | return false; |
| 279 | } |
| 280 | } |
| 281 | else if (type == acpi_state::PowerStateType::devPowerState) |
| 282 | { |
| 283 | if ((state <= static_cast<uint8_t>(acpi_state::PowerState::s3D3)) || |
| 284 | (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) || |
| 285 | (state == static_cast<uint8_t>(acpi_state::PowerState::noChange))) |
| 286 | { |
| 287 | return true; |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | return false; |
| 292 | } |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | return false; |
| 297 | } |
| 298 | return false; |
| 299 | } |
| 300 | } // namespace acpi_state |
| 301 | |
Adriana Kobylak | 3a552e1 | 2015-10-19 16:11:00 -0500 | [diff] [blame] | 302 | ipmi_ret_t ipmi_app_set_acpi_power_state(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 303 | ipmi_request_t request, |
| 304 | ipmi_response_t response, |
| 305 | ipmi_data_len_t data_len, |
| 306 | ipmi_context_t context) |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 307 | { |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 308 | auto s = static_cast<uint8_t>(acpi_state::PowerState::unknown); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 309 | ipmi_ret_t rc = IPMI_CC_OK; |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 310 | |
| 311 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 312 | |
| 313 | auto value = acpi_state::ACPIPowerState::ACPI::Unknown; |
| 314 | |
| 315 | auto* req = reinterpret_cast<acpi_state::ACPIState*>(request); |
| 316 | |
| 317 | if (*data_len != sizeof(acpi_state::ACPIState)) |
| 318 | { |
| 319 | log<level::ERR>("set_acpi invalid len"); |
| 320 | *data_len = 0; |
| 321 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 322 | } |
| 323 | |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 324 | *data_len = 0; |
| 325 | |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 326 | if (req->sysACPIState & acpi_state::stateChanged) |
| 327 | { |
| 328 | // set system power state |
| 329 | s = req->sysACPIState & ~acpi_state::stateChanged; |
| 330 | |
| 331 | if (!acpi_state::isValidACPIState( |
| 332 | acpi_state::PowerStateType::sysPowerState, s)) |
| 333 | { |
| 334 | log<level::ERR>("set_acpi_power sys invalid input", |
| 335 | entry("S=%x", s)); |
| 336 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 337 | } |
| 338 | |
| 339 | // valid input |
| 340 | if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange)) |
| 341 | { |
| 342 | log<level::DEBUG>("No change for system power state"); |
| 343 | } |
| 344 | else |
| 345 | { |
| 346 | auto found = std::find_if( |
| 347 | acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(), |
| 348 | [&s](const auto& iter) { |
| 349 | return (static_cast<uint8_t>(iter.second) == s); |
| 350 | }); |
| 351 | |
| 352 | value = found->first; |
| 353 | |
| 354 | try |
| 355 | { |
| 356 | auto acpiObject = |
| 357 | ipmi::getDbusObject(bus, acpi_state::acpiInterface); |
| 358 | ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first, |
| 359 | acpi_state::acpiInterface, |
| 360 | acpi_state::sysACPIProp, |
| 361 | convertForMessage(value)); |
| 362 | } |
| 363 | catch (const InternalFailure& e) |
| 364 | { |
| 365 | log<level::ERR>("Failed in set ACPI system property", |
| 366 | entry("EXCEPTION=%s", e.what())); |
| 367 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | log<level::DEBUG>("Do not change system power state"); |
| 374 | } |
| 375 | |
| 376 | if (req->devACPIState & acpi_state::stateChanged) |
| 377 | { |
| 378 | // set device power state |
| 379 | s = req->devACPIState & ~acpi_state::stateChanged; |
| 380 | if (!acpi_state::isValidACPIState( |
| 381 | acpi_state::PowerStateType::devPowerState, s)) |
| 382 | { |
| 383 | log<level::ERR>("set_acpi_power dev invalid input", |
| 384 | entry("S=%x", s)); |
| 385 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 386 | } |
| 387 | |
| 388 | // valid input |
| 389 | if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange)) |
| 390 | { |
| 391 | log<level::DEBUG>("No change for device power state"); |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | auto found = std::find_if( |
| 396 | acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(), |
| 397 | [&s](const auto& iter) { |
| 398 | return (static_cast<uint8_t>(iter.second) == s); |
| 399 | }); |
| 400 | |
| 401 | value = found->first; |
| 402 | |
| 403 | try |
| 404 | { |
| 405 | auto acpiObject = |
| 406 | ipmi::getDbusObject(bus, acpi_state::acpiInterface); |
| 407 | ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first, |
| 408 | acpi_state::acpiInterface, |
| 409 | acpi_state::devACPIProp, |
| 410 | convertForMessage(value)); |
| 411 | } |
| 412 | catch (const InternalFailure& e) |
| 413 | { |
| 414 | log<level::ERR>("Failed in set ACPI device property", |
| 415 | entry("EXCEPTION=%s", e.what())); |
| 416 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | else |
| 421 | { |
| 422 | log<level::DEBUG>("Do not change device power state"); |
| 423 | } |
| 424 | |
| 425 | return rc; |
| 426 | } |
| 427 | |
Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 428 | /** |
| 429 | * @brief implements the get ACPI power state command |
| 430 | * |
| 431 | * @return IPMI completion code plus response data on success. |
| 432 | * - ACPI system power state |
| 433 | * - ACPI device power state |
| 434 | **/ |
| 435 | ipmi::RspType<uint8_t, // acpiSystemPowerState |
| 436 | uint8_t // acpiDevicePowerState |
| 437 | > |
| 438 | ipmiGetAcpiPowerState() |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 439 | { |
Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 440 | uint8_t sysAcpiState; |
| 441 | uint8_t devAcpiState; |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 442 | |
| 443 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 444 | |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 445 | try |
| 446 | { |
| 447 | auto acpiObject = ipmi::getDbusObject(bus, acpi_state::acpiInterface); |
| 448 | |
| 449 | auto sysACPIVal = ipmi::getDbusProperty( |
| 450 | bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface, |
| 451 | acpi_state::sysACPIProp); |
| 452 | auto sysACPI = acpi_state::ACPIPowerState::convertACPIFromString( |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 453 | std::get<std::string>(sysACPIVal)); |
Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 454 | sysAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(sysACPI)); |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 455 | |
| 456 | auto devACPIVal = ipmi::getDbusProperty( |
| 457 | bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface, |
| 458 | acpi_state::devACPIProp); |
| 459 | auto devACPI = acpi_state::ACPIPowerState::convertACPIFromString( |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 460 | std::get<std::string>(devACPIVal)); |
Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 461 | devAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(devACPI)); |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 462 | } |
| 463 | catch (const InternalFailure& e) |
| 464 | { |
Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 465 | return ipmi::responseUnspecifiedError(); |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 466 | } |
Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 467 | |
| 468 | return ipmi::responseSuccess(sysAcpiState, devAcpiState); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 469 | } |
| 470 | |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 471 | typedef struct |
| 472 | { |
| 473 | char major; |
| 474 | char minor; |
Chris Austen | 176c965 | 2016-04-30 16:32:17 -0500 | [diff] [blame] | 475 | uint16_t d[2]; |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 476 | } Revision; |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 477 | |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 478 | /* Currently supports the vx.x-x-[-x] and v1.x.x-x-[-x] format. It will */ |
| 479 | /* return -1 if not in those formats, this routine knows how to parse */ |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 480 | /* version = v0.6-19-gf363f61-dirty */ |
| 481 | /* ^ ^ ^^ ^ */ |
| 482 | /* | | |----------|-- additional details */ |
| 483 | /* | |---------------- Minor */ |
| 484 | /* |------------------ Major */ |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 485 | /* and version = v1.99.10-113-g65edf7d-r3-0-g9e4f715 */ |
| 486 | /* ^ ^ ^^ ^ */ |
| 487 | /* | | |--|---------- additional details */ |
| 488 | /* | |---------------- Minor */ |
| 489 | /* |------------------ Major */ |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 490 | /* Additional details : If the option group exists it will force Auxiliary */ |
| 491 | /* Firmware Revision Information 4th byte to 1 indicating the build was */ |
| 492 | /* derived with additional edits */ |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 493 | int convertVersion(std::string s, Revision& rev) |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 494 | { |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 495 | std::string token; |
Chris Austen | 176c965 | 2016-04-30 16:32:17 -0500 | [diff] [blame] | 496 | uint16_t commits; |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 497 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 498 | auto location = s.find_first_of('v'); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 499 | if (location != std::string::npos) |
| 500 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 501 | s = s.substr(location + 1); |
Chris Austen | 176c965 | 2016-04-30 16:32:17 -0500 | [diff] [blame] | 502 | } |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 503 | |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 504 | if (!s.empty()) |
| 505 | { |
| 506 | location = s.find_first_of("."); |
| 507 | if (location != std::string::npos) |
| 508 | { |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 509 | rev.major = |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 510 | static_cast<char>(std::stoi(s.substr(0, location), 0, 16)); |
| 511 | token = s.substr(location + 1); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 512 | } |
Chris Austen | 176c965 | 2016-04-30 16:32:17 -0500 | [diff] [blame] | 513 | |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 514 | if (!token.empty()) |
| 515 | { |
| 516 | location = token.find_first_of(".-"); |
| 517 | if (location != std::string::npos) |
| 518 | { |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 519 | rev.minor = static_cast<char>( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 520 | std::stoi(token.substr(0, location), 0, 16)); |
| 521 | token = token.substr(location + 1); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 522 | } |
| 523 | } |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 524 | |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 525 | // Capture the number of commits on top of the minor tag. |
| 526 | // I'm using BE format like the ipmi spec asked for |
| 527 | location = token.find_first_of(".-"); |
| 528 | if (!token.empty()) |
| 529 | { |
| 530 | commits = std::stoi(token.substr(0, location), 0, 16); |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 531 | rev.d[0] = (commits >> 8) | (commits << 8); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 532 | |
| 533 | // commit number we skip |
| 534 | location = token.find_first_of(".-"); |
| 535 | if (location != std::string::npos) |
| 536 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 537 | token = token.substr(location + 1); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 538 | } |
| 539 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 540 | else |
| 541 | { |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 542 | rev.d[0] = 0; |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | if (location != std::string::npos) |
| 546 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 547 | token = token.substr(location + 1); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | // Any value of the optional parameter forces it to 1 |
| 551 | location = token.find_first_of(".-"); |
| 552 | if (location != std::string::npos) |
| 553 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 554 | token = token.substr(location + 1); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 555 | } |
| 556 | commits = (!token.empty()) ? 1 : 0; |
| 557 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 558 | // We do this operation to get this displayed in least significant bytes |
| 559 | // of ipmitool device id command. |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 560 | rev.d[1] = (commits >> 8) | (commits << 8); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 561 | } |
| 562 | |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
Vernon Mauery | ea1c401 | 2019-05-24 13:26:16 -0700 | [diff] [blame] | 566 | /* @brief: Implement the Get Device ID IPMI command per the IPMI spec |
| 567 | * @param[in] ctx - shared_ptr to an IPMI context struct |
| 568 | * |
| 569 | * @returns IPMI completion code plus response data |
| 570 | * - Device ID (manufacturer defined) |
| 571 | * - Device revision[4 bits]; reserved[3 bits]; SDR support[1 bit] |
| 572 | * - FW revision major[7 bits] (binary encoded); available[1 bit] |
| 573 | * - FW Revision minor (BCD encoded) |
| 574 | * - IPMI version (0x02 for IPMI 2.0) |
| 575 | * - device support (bitfield of supported options) |
| 576 | * - MFG IANA ID (3 bytes) |
| 577 | * - product ID (2 bytes) |
| 578 | * - AUX info (4 bytes) |
| 579 | */ |
| 580 | ipmi::RspType<uint8_t, // Device ID |
| 581 | uint8_t, // Device Revision |
| 582 | uint8_t, // Firmware Revision Major |
| 583 | uint8_t, // Firmware Revision minor |
| 584 | uint8_t, // IPMI version |
| 585 | uint8_t, // Additional device support |
| 586 | uint24_t, // MFG ID |
| 587 | uint16_t, // Product ID |
| 588 | uint32_t // AUX info |
| 589 | > |
| 590 | ipmiAppGetDeviceId(ipmi::Context::ptr ctx) |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 591 | { |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 592 | int r = -1; |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 593 | Revision rev = {0}; |
| 594 | static struct |
| 595 | { |
| 596 | uint8_t id; |
| 597 | uint8_t revision; |
| 598 | uint8_t fw[2]; |
| 599 | uint8_t ipmiVer; |
| 600 | uint8_t addnDevSupport; |
| 601 | uint24_t manufId; |
| 602 | uint16_t prodId; |
| 603 | uint32_t aux; |
| 604 | } devId; |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 605 | static bool dev_id_initialized = false; |
Patrick Venture | 94930a1 | 2019-04-30 10:01:58 -0700 | [diff] [blame] | 606 | static bool defaultActivationSetting = true; |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 607 | const char* filename = "/usr/share/ipmi-providers/dev_id.json"; |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 608 | constexpr auto ipmiDevIdStateShift = 7; |
| 609 | constexpr auto ipmiDevIdFw1Mask = ~(1 << ipmiDevIdStateShift); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 610 | |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 611 | if (!dev_id_initialized) |
| 612 | { |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 613 | try |
| 614 | { |
Vernon Mauery | ea1c401 | 2019-05-24 13:26:16 -0700 | [diff] [blame] | 615 | auto version = getActiveSoftwareVersionInfo(ctx); |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 616 | r = convertVersion(version, rev); |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 617 | } |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 618 | catch (const std::exception& e) |
| 619 | { |
| 620 | log<level::ERR>(e.what()); |
| 621 | } |
Nan Li | ee0cb90 | 2016-07-11 15:38:03 +0800 | [diff] [blame] | 622 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 623 | if (r >= 0) |
| 624 | { |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 625 | // bit7 identifies if the device is available |
| 626 | // 0=normal operation |
| 627 | // 1=device firmware, SDR update, |
| 628 | // or self-initialization in progress. |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 629 | // The availability may change in run time, so mask here |
| 630 | // and initialize later. |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 631 | devId.fw[0] = rev.major & ipmiDevIdFw1Mask; |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 632 | |
| 633 | rev.minor = (rev.minor > 99 ? 99 : rev.minor); |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 634 | devId.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16; |
| 635 | std::memcpy(&devId.aux, rev.d, 4); |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 636 | } |
Nan Li | ee0cb90 | 2016-07-11 15:38:03 +0800 | [diff] [blame] | 637 | |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 638 | // IPMI Spec version 2.0 |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 639 | devId.ipmiVer = 2; |
Adriana Kobylak | 0e91264 | 2016-06-22 16:54:39 -0500 | [diff] [blame] | 640 | |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 641 | std::ifstream devIdFile(filename); |
| 642 | if (devIdFile.is_open()) |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 643 | { |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 644 | auto data = nlohmann::json::parse(devIdFile, nullptr, false); |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 645 | if (!data.is_discarded()) |
| 646 | { |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 647 | devId.id = data.value("id", 0); |
| 648 | devId.revision = data.value("revision", 0); |
| 649 | devId.addnDevSupport = data.value("addn_dev_support", 0); |
| 650 | devId.manufId = data.value("manuf_id", 0); |
| 651 | devId.prodId = data.value("prod_id", 0); |
| 652 | devId.aux = data.value("aux", 0); |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 653 | |
Patrick Venture | 94930a1 | 2019-04-30 10:01:58 -0700 | [diff] [blame] | 654 | // Set the availablitity of the BMC. |
| 655 | defaultActivationSetting = data.value("availability", true); |
| 656 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 657 | // Don't read the file every time if successful |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 658 | dev_id_initialized = true; |
| 659 | } |
| 660 | else |
| 661 | { |
| 662 | log<level::ERR>("Device ID JSON parser failure"); |
Vernon Mauery | f2587fc | 2019-04-09 14:28:15 -0700 | [diff] [blame] | 663 | return ipmi::responseUnspecifiedError(); |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | else |
| 667 | { |
| 668 | log<level::ERR>("Device ID file not found"); |
Vernon Mauery | f2587fc | 2019-04-09 14:28:15 -0700 | [diff] [blame] | 669 | return ipmi::responseUnspecifiedError(); |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 670 | } |
| 671 | } |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 672 | |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 673 | // Set availability to the actual current BMC state |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 674 | devId.fw[0] &= ipmiDevIdFw1Mask; |
Patrick Venture | 94930a1 | 2019-04-30 10:01:58 -0700 | [diff] [blame] | 675 | if (!getCurrentBmcStateWithFallback(defaultActivationSetting)) |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 676 | { |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 677 | devId.fw[0] |= (1 << ipmiDevIdStateShift); |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 678 | } |
| 679 | |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 680 | return ipmi::responseSuccess( |
| 681 | devId.id, devId.revision, devId.fw[0], devId.fw[1], devId.ipmiVer, |
| 682 | devId.addnDevSupport, devId.manufId, devId.prodId, devId.aux); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 683 | } |
| 684 | |
Vernon Mauery | b84a528 | 2019-03-25 13:39:03 -0700 | [diff] [blame] | 685 | auto ipmiAppGetSelfTestResults() -> ipmi::RspType<uint8_t, uint8_t> |
Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 686 | { |
Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 687 | // Byte 2: |
| 688 | // 55h - No error. |
Gunnar Mills | 8991dd6 | 2017-10-25 17:11:29 -0500 | [diff] [blame] | 689 | // 56h - Self Test function not implemented in this controller. |
Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 690 | // 57h - Corrupted or inaccesssible data or devices. |
| 691 | // 58h - Fatal hardware error. |
| 692 | // FFh - reserved. |
| 693 | // all other: Device-specific 'internal failure'. |
| 694 | // Byte 3: |
| 695 | // For byte 2 = 55h, 56h, FFh: 00h |
| 696 | // For byte 2 = 58h, all other: Device-specific |
| 697 | // For byte 2 = 57h: self-test error bitfield. |
| 698 | // Note: returning 57h does not imply that all test were run. |
| 699 | // [7] 1b = Cannot access SEL device. |
| 700 | // [6] 1b = Cannot access SDR Repository. |
| 701 | // [5] 1b = Cannot access BMC FRU device. |
| 702 | // [4] 1b = IPMB signal lines do not respond. |
| 703 | // [3] 1b = SDR Repository empty. |
| 704 | // [2] 1b = Internal Use Area of BMC FRU corrupted. |
| 705 | // [1] 1b = controller update 'boot block' firmware corrupted. |
| 706 | // [0] 1b = controller operational firmware corrupted. |
Vernon Mauery | b84a528 | 2019-03-25 13:39:03 -0700 | [diff] [blame] | 707 | constexpr uint8_t notImplemented = 0x56; |
| 708 | constexpr uint8_t zero = 0; |
| 709 | return ipmi::responseSuccess(notImplemented, zero); |
Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 710 | } |
| 711 | |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 712 | static constexpr size_t uuidBinaryLength = 16; |
| 713 | static std::array<uint8_t, uuidBinaryLength> rfc4122ToIpmi(std::string rfc4122) |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 714 | { |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 715 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 716 | // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223 |
Patrick Venture | d211702 | 2018-02-06 08:54:37 -0800 | [diff] [blame] | 717 | // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte |
| 718 | // order |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 719 | // Ex: 0x2332fc2c40e66298e511f2782395a361 |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 720 | constexpr size_t uuidHexLength = (2 * uuidBinaryLength); |
| 721 | constexpr size_t uuidRfc4122Length = (uuidHexLength + 4); |
| 722 | std::array<uint8_t, uuidBinaryLength> uuid; |
| 723 | if (rfc4122.size() == uuidRfc4122Length) |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 724 | { |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 725 | rfc4122.erase(std::remove(rfc4122.begin(), rfc4122.end(), '-'), |
| 726 | rfc4122.end()); |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 727 | } |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 728 | if (rfc4122.size() != uuidHexLength) |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 729 | { |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 730 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"), |
| 731 | Argument::ARGUMENT_VALUE(rfc4122.c_str())); |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 732 | } |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 733 | for (size_t ind = 0; ind < uuidHexLength; ind += 2) |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 734 | { |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 735 | char v[3]; |
| 736 | v[0] = rfc4122[ind]; |
| 737 | v[1] = rfc4122[ind + 1]; |
| 738 | v[2] = 0; |
| 739 | size_t err; |
| 740 | long b; |
| 741 | try |
Emily Shaffer | edb8bb0 | 2018-09-27 14:50:15 -0700 | [diff] [blame] | 742 | { |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 743 | b = std::stoul(v, &err, 16); |
Emily Shaffer | edb8bb0 | 2018-09-27 14:50:15 -0700 | [diff] [blame] | 744 | } |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 745 | catch (std::exception& e) |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 746 | { |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 747 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"), |
| 748 | Argument::ARGUMENT_VALUE(rfc4122.c_str())); |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 749 | } |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 750 | // check that exactly two ascii bytes were converted |
| 751 | if (err != 2) |
| 752 | { |
| 753 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"), |
| 754 | Argument::ARGUMENT_VALUE(rfc4122.c_str())); |
| 755 | } |
| 756 | uuid[uuidBinaryLength - (ind / 2) - 1] = static_cast<uint8_t>(b); |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 757 | } |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 758 | return uuid; |
| 759 | } |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 760 | |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 761 | auto ipmiAppGetDeviceGuid() |
| 762 | -> ipmi::RspType<std::array<uint8_t, uuidBinaryLength>> |
| 763 | { |
| 764 | // return a fixed GUID based on /etc/machine-id |
| 765 | // This should match the /redfish/v1/Managers/bmc's UUID data |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 766 | |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 767 | // machine specific application ID (for BMC ID) |
| 768 | // generated by systemd-id128 -p new as per man page |
| 769 | static constexpr sd_id128_t bmcUuidAppId = SD_ID128_MAKE( |
| 770 | 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] | 771 | |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 772 | sd_id128_t bmcUuid; |
| 773 | // create the UUID from /etc/machine-id via the systemd API |
| 774 | sd_id128_get_machine_app_specific(bmcUuidAppId, &bmcUuid); |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 775 | |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 776 | char bmcUuidCstr[SD_ID128_STRING_MAX]; |
| 777 | std::string systemUuid = sd_id128_to_string(bmcUuid, bmcUuidCstr); |
| 778 | |
| 779 | std::array<uint8_t, uuidBinaryLength> uuid = rfc4122ToIpmi(systemUuid); |
| 780 | return ipmi::responseSuccess(uuid); |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 781 | } |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 782 | |
Vernon Mauery | cc99ba4 | 2019-03-25 13:40:11 -0700 | [diff] [blame] | 783 | auto ipmiAppGetBtCapabilities() |
| 784 | -> ipmi::RspType<uint8_t, uint8_t, uint8_t, uint8_t, uint8_t> |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 785 | { |
Adriana Kobylak | 88ad815 | 2016-12-13 10:09:08 -0600 | [diff] [blame] | 786 | // Per IPMI 2.0 spec, the input and output buffer size must be the max |
| 787 | // buffer size minus one byte to allocate space for the length byte. |
Vernon Mauery | cc99ba4 | 2019-03-25 13:40:11 -0700 | [diff] [blame] | 788 | constexpr uint8_t nrOutstanding = 0x01; |
| 789 | constexpr uint8_t inputBufferSize = MAX_IPMI_BUFFER - 1; |
| 790 | constexpr uint8_t outputBufferSize = MAX_IPMI_BUFFER - 1; |
| 791 | constexpr uint8_t transactionTime = 0x0A; |
| 792 | constexpr uint8_t nrRetries = 0x01; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 793 | |
Vernon Mauery | cc99ba4 | 2019-03-25 13:40:11 -0700 | [diff] [blame] | 794 | return ipmi::responseSuccess(nrOutstanding, inputBufferSize, |
| 795 | outputBufferSize, transactionTime, nrRetries); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 796 | } |
| 797 | |
Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 798 | auto ipmiAppGetSystemGuid() -> ipmi::RspType<std::array<uint8_t, 16>> |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 799 | { |
Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 800 | static constexpr auto bmcInterface = |
| 801 | "xyz.openbmc_project.Inventory.Item.Bmc"; |
| 802 | static constexpr auto uuidInterface = "xyz.openbmc_project.Common.UUID"; |
| 803 | static constexpr auto uuidProperty = "UUID"; |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 804 | |
Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 805 | ipmi::Value propValue; |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 806 | try |
| 807 | { |
| 808 | // Get the Inventory object implementing BMC interface |
Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 809 | auto busPtr = getSdBus(); |
| 810 | auto objectInfo = ipmi::getDbusObject(*busPtr, bmcInterface); |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 811 | |
| 812 | // Read UUID property value from bmcObject |
| 813 | // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223 |
Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 814 | propValue = |
| 815 | ipmi::getDbusProperty(*busPtr, objectInfo.second, objectInfo.first, |
| 816 | uuidInterface, uuidProperty); |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 817 | } |
| 818 | catch (const InternalFailure& e) |
| 819 | { |
| 820 | log<level::ERR>("Failed in reading BMC UUID property", |
Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 821 | entry("INTERFACE=%s", uuidInterface), |
| 822 | entry("PROPERTY=%s", uuidProperty)); |
| 823 | return ipmi::responseUnspecifiedError(); |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 824 | } |
Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 825 | std::array<uint8_t, 16> uuid; |
| 826 | std::string rfc4122Uuid = std::get<std::string>(propValue); |
| 827 | try |
| 828 | { |
| 829 | // convert to IPMI format |
| 830 | uuid = rfc4122ToIpmi(rfc4122Uuid); |
| 831 | } |
| 832 | catch (const InvalidArgument& e) |
| 833 | { |
| 834 | log<level::ERR>("Failed in parsing BMC UUID property", |
| 835 | entry("INTERFACE=%s", uuidInterface), |
| 836 | entry("PROPERTY=%s", uuidProperty), |
| 837 | entry("VALUE=%s", rfc4122Uuid.c_str())); |
| 838 | return ipmi::responseUnspecifiedError(); |
| 839 | } |
| 840 | return ipmi::responseSuccess(uuid); |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 841 | } |
| 842 | |
Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 843 | static std::unique_ptr<SysInfoParamStore> sysInfoParamStore; |
| 844 | |
Xo Wang | 8765133 | 2017-08-11 10:17:59 -0700 | [diff] [blame] | 845 | static std::string sysInfoReadSystemName() |
| 846 | { |
| 847 | // Use the BMC hostname as the "System Name." |
| 848 | char hostname[HOST_NAME_MAX + 1] = {}; |
| 849 | if (gethostname(hostname, HOST_NAME_MAX) != 0) |
| 850 | { |
| 851 | perror("System info parameter: system name"); |
| 852 | } |
| 853 | return hostname; |
| 854 | } |
| 855 | |
Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 856 | struct IpmiSysInfoResp |
| 857 | { |
| 858 | uint8_t paramRevision; |
| 859 | uint8_t setSelector; |
| 860 | union |
| 861 | { |
| 862 | struct |
| 863 | { |
| 864 | uint8_t encoding; |
| 865 | uint8_t stringLen; |
| 866 | uint8_t stringData0[14]; |
| 867 | } __attribute__((packed)); |
| 868 | uint8_t stringDataN[16]; |
| 869 | uint8_t byteData; |
| 870 | }; |
| 871 | } __attribute__((packed)); |
| 872 | |
| 873 | /** |
| 874 | * Split a string into (up to) 16-byte chunks as expected in response for get |
| 875 | * system info parameter. |
| 876 | * |
| 877 | * @param[in] fullString: Input string to be split |
| 878 | * @param[in] chunkIndex: Index of the chunk to be written out |
| 879 | * @param[in,out] chunk: Output data buffer; must have 14 byte capacity if |
| 880 | * chunk_index = 0 and 16-byte capacity otherwise |
| 881 | * @return the number of bytes written into the output buffer, or -EINVAL for |
| 882 | * invalid arguments. |
| 883 | */ |
| 884 | static int splitStringParam(const std::string& fullString, int chunkIndex, |
| 885 | uint8_t* chunk) |
| 886 | { |
| 887 | constexpr int maxChunk = 255; |
| 888 | constexpr int smallChunk = 14; |
| 889 | constexpr int chunkSize = 16; |
| 890 | if (chunkIndex > maxChunk || chunk == nullptr) |
| 891 | { |
| 892 | return -EINVAL; |
| 893 | } |
| 894 | try |
| 895 | { |
| 896 | std::string output; |
| 897 | if (chunkIndex == 0) |
| 898 | { |
| 899 | // Output must have 14 byte capacity. |
| 900 | output = fullString.substr(0, smallChunk); |
| 901 | } |
| 902 | else |
| 903 | { |
| 904 | // Output must have 16 byte capacity. |
| 905 | output = fullString.substr((chunkIndex * chunkSize) - 2, chunkSize); |
| 906 | } |
| 907 | |
| 908 | std::memcpy(chunk, output.c_str(), output.length()); |
| 909 | return output.length(); |
| 910 | } |
| 911 | catch (const std::out_of_range& e) |
| 912 | { |
| 913 | // The position was beyond the end. |
| 914 | return -EINVAL; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * Packs the Get Sys Info Request Item into the response. |
| 920 | * |
| 921 | * @param[in] paramString - the parameter. |
| 922 | * @param[in] setSelector - the selector |
| 923 | * @param[in,out] resp - the System info response. |
| 924 | * @return The number of bytes packed or failure from splitStringParam(). |
| 925 | */ |
| 926 | static int packGetSysInfoResp(const std::string& paramString, |
| 927 | uint8_t setSelector, IpmiSysInfoResp* resp) |
| 928 | { |
| 929 | uint8_t* dataBuffer = resp->stringDataN; |
| 930 | resp->setSelector = setSelector; |
| 931 | if (resp->setSelector == 0) // First chunk has only 14 bytes. |
| 932 | { |
| 933 | resp->encoding = 0; |
| 934 | resp->stringLen = paramString.length(); |
| 935 | dataBuffer = resp->stringData0; |
| 936 | } |
| 937 | return splitStringParam(paramString, resp->setSelector, dataBuffer); |
| 938 | } |
| 939 | |
| 940 | ipmi_ret_t ipmi_app_get_system_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 941 | ipmi_request_t request, |
| 942 | ipmi_response_t response, |
| 943 | ipmi_data_len_t dataLen, |
| 944 | ipmi_context_t context) |
| 945 | { |
| 946 | IpmiSysInfoResp resp = {}; |
| 947 | size_t respLen = 0; |
| 948 | uint8_t* const reqData = static_cast<uint8_t*>(request); |
| 949 | std::string paramString; |
| 950 | bool found; |
| 951 | std::tuple<bool, std::string> ret; |
| 952 | constexpr int minRequestSize = 4; |
| 953 | constexpr int paramSelector = 1; |
| 954 | constexpr uint8_t revisionOnly = 0x80; |
| 955 | const uint8_t paramRequested = reqData[paramSelector]; |
| 956 | int rc; |
| 957 | |
| 958 | if (*dataLen < minRequestSize) |
| 959 | { |
| 960 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 961 | } |
| 962 | |
| 963 | *dataLen = 0; // default to 0. |
| 964 | |
| 965 | // Parameters revision as of IPMI spec v2.0 rev. 1.1 (Feb 11, 2014 E6) |
| 966 | resp.paramRevision = 0x11; |
| 967 | if (reqData[0] & revisionOnly) // Get parameter revision only |
| 968 | { |
| 969 | respLen = 1; |
| 970 | goto writeResponse; |
| 971 | } |
| 972 | |
| 973 | // The "Set In Progress" parameter can be used for rollback of parameter |
| 974 | // data and is not implemented. |
| 975 | if (paramRequested == 0) |
| 976 | { |
| 977 | resp.byteData = 0; |
| 978 | respLen = 2; |
| 979 | goto writeResponse; |
| 980 | } |
| 981 | |
| 982 | if (sysInfoParamStore == nullptr) |
| 983 | { |
| 984 | sysInfoParamStore = std::make_unique<SysInfoParamStore>(); |
Xo Wang | 8765133 | 2017-08-11 10:17:59 -0700 | [diff] [blame] | 985 | sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME, |
| 986 | sysInfoReadSystemName); |
Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | // Parameters other than Set In Progress are assumed to be strings. |
| 990 | ret = sysInfoParamStore->lookup(paramRequested); |
| 991 | found = std::get<0>(ret); |
| 992 | paramString = std::get<1>(ret); |
| 993 | if (!found) |
| 994 | { |
| 995 | return IPMI_CC_SYSTEM_INFO_PARAMETER_NOT_SUPPORTED; |
| 996 | } |
| 997 | // TODO: Cache each parameter across multiple calls, until the whole string |
| 998 | // has been read out. Otherwise, it's possible for a parameter to change |
| 999 | // between requests for its chunks, returning chunks incoherent with each |
| 1000 | // other. For now, the parameter store is simply required to have only |
| 1001 | // idempotent callbacks. |
| 1002 | rc = packGetSysInfoResp(paramString, reqData[2], &resp); |
| 1003 | if (rc == -EINVAL) |
| 1004 | { |
| 1005 | return IPMI_CC_RESPONSE_ERROR; |
| 1006 | } |
| 1007 | |
| 1008 | respLen = sizeof(resp); // Write entire string data chunk in response. |
| 1009 | |
| 1010 | writeResponse: |
| 1011 | std::memcpy(response, &resp, sizeof(resp)); |
| 1012 | *dataLen = respLen; |
| 1013 | return IPMI_CC_OK; |
| 1014 | } |
| 1015 | |
Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1016 | inline std::vector<uint8_t> convertStringToData(const std::string& command) |
| 1017 | { |
| 1018 | std::istringstream iss(command); |
| 1019 | std::string token; |
| 1020 | std::vector<uint8_t> dataValue; |
| 1021 | while (std::getline(iss, token, ' ')) |
| 1022 | { |
| 1023 | dataValue.emplace_back( |
| 1024 | static_cast<uint8_t>(std::stoul(token, nullptr, base_16))); |
| 1025 | } |
| 1026 | return dataValue; |
| 1027 | } |
| 1028 | |
| 1029 | static bool populateI2CMasterWRWhitelist() |
| 1030 | { |
| 1031 | nlohmann::json data = nullptr; |
| 1032 | std::ifstream jsonFile(i2cMasterWRWhitelistFile); |
| 1033 | |
| 1034 | if (!jsonFile.good()) |
| 1035 | { |
| 1036 | log<level::WARNING>("i2c white list file not found!", |
| 1037 | entry("FILE_NAME: %s", i2cMasterWRWhitelistFile)); |
| 1038 | return false; |
| 1039 | } |
| 1040 | |
| 1041 | try |
| 1042 | { |
| 1043 | data = nlohmann::json::parse(jsonFile, nullptr, false); |
| 1044 | } |
| 1045 | catch (nlohmann::json::parse_error& e) |
| 1046 | { |
| 1047 | log<level::ERR>("Corrupted i2c white list config file", |
| 1048 | entry("FILE_NAME: %s", i2cMasterWRWhitelistFile), |
| 1049 | entry("MSG: %s", e.what())); |
| 1050 | return false; |
| 1051 | } |
| 1052 | |
| 1053 | try |
| 1054 | { |
| 1055 | // Example JSON Structure format |
| 1056 | // "filters": [ |
| 1057 | // { |
| 1058 | // "Description": "Allow full read - ignore first byte write value |
| 1059 | // for 0x40 to 0x4F", |
| 1060 | // "busId": "0x01", |
| 1061 | // "slaveAddr": "0x40", |
| 1062 | // "slaveAddrMask": "0x0F", |
| 1063 | // "command": "0x00", |
| 1064 | // "commandMask": "0xFF" |
| 1065 | // }, |
| 1066 | // { |
| 1067 | // "Description": "Allow full read - first byte match 0x05 and |
| 1068 | // ignore second byte", |
| 1069 | // "busId": "0x01", |
| 1070 | // "slaveAddr": "0x57", |
| 1071 | // "slaveAddrMask": "0x00", |
| 1072 | // "command": "0x05 0x00", |
| 1073 | // "commandMask": "0x00 0xFF" |
| 1074 | // },] |
| 1075 | |
| 1076 | nlohmann::json filters = data[filtersStr].get<nlohmann::json>(); |
| 1077 | std::vector<i2cMasterWRWhitelist>& whitelist = getWRWhitelist(); |
| 1078 | for (const auto& it : filters.items()) |
| 1079 | { |
| 1080 | nlohmann::json filter = it.value(); |
| 1081 | if (filter.is_null()) |
| 1082 | { |
| 1083 | log<level::ERR>( |
| 1084 | "Corrupted I2C master write read whitelist config file", |
| 1085 | entry("FILE_NAME: %s", i2cMasterWRWhitelistFile)); |
| 1086 | return false; |
| 1087 | } |
| 1088 | const std::vector<uint8_t>& writeData = |
| 1089 | convertStringToData(filter[cmdStr].get<std::string>()); |
| 1090 | const std::vector<uint8_t>& writeDataMask = |
| 1091 | convertStringToData(filter[cmdMaskStr].get<std::string>()); |
| 1092 | if (writeDataMask.size() != writeData.size()) |
| 1093 | { |
| 1094 | log<level::ERR>("I2C master write read whitelist filter " |
| 1095 | "mismatch for command & mask size"); |
| 1096 | return false; |
| 1097 | } |
| 1098 | whitelist.push_back( |
| 1099 | {static_cast<uint8_t>(std::stoul( |
| 1100 | filter[busIdStr].get<std::string>(), nullptr, base_16)), |
| 1101 | static_cast<uint8_t>( |
| 1102 | std::stoul(filter[slaveAddrStr].get<std::string>(), |
| 1103 | nullptr, base_16)), |
| 1104 | static_cast<uint8_t>( |
| 1105 | std::stoul(filter[slaveAddrMaskStr].get<std::string>(), |
| 1106 | nullptr, base_16)), |
| 1107 | writeData, writeDataMask}); |
| 1108 | } |
| 1109 | if (whitelist.size() != filters.size()) |
| 1110 | { |
| 1111 | log<level::ERR>( |
| 1112 | "I2C master write read whitelist filter size mismatch"); |
| 1113 | return false; |
| 1114 | } |
| 1115 | } |
| 1116 | catch (std::exception& e) |
| 1117 | { |
| 1118 | log<level::ERR>("I2C master write read whitelist unexpected exception", |
| 1119 | entry("ERROR=%s", e.what())); |
| 1120 | return false; |
| 1121 | } |
| 1122 | return true; |
| 1123 | } |
| 1124 | |
| 1125 | static inline bool isWriteDataWhitelisted(const std::vector<uint8_t>& data, |
| 1126 | const std::vector<uint8_t>& dataMask, |
| 1127 | const std::vector<uint8_t>& writeData) |
| 1128 | { |
| 1129 | std::vector<uint8_t> processedDataBuf(data.size()); |
| 1130 | std::vector<uint8_t> processedReqBuf(dataMask.size()); |
| 1131 | std::transform(writeData.begin(), writeData.end(), dataMask.begin(), |
| 1132 | processedReqBuf.begin(), std::bit_or<uint8_t>()); |
| 1133 | std::transform(data.begin(), data.end(), dataMask.begin(), |
| 1134 | processedDataBuf.begin(), std::bit_or<uint8_t>()); |
| 1135 | |
| 1136 | return (processedDataBuf == processedReqBuf); |
| 1137 | } |
| 1138 | |
| 1139 | static bool isCmdWhitelisted(uint8_t busId, uint8_t slaveAddr, |
| 1140 | std::vector<uint8_t>& writeData) |
| 1141 | { |
| 1142 | std::vector<i2cMasterWRWhitelist>& whiteList = getWRWhitelist(); |
| 1143 | for (const auto& wlEntry : whiteList) |
| 1144 | { |
| 1145 | if ((busId == wlEntry.busId) && |
| 1146 | ((slaveAddr | wlEntry.slaveAddrMask) == |
| 1147 | (wlEntry.slaveAddr | wlEntry.slaveAddrMask))) |
| 1148 | { |
| 1149 | const std::vector<uint8_t>& dataMask = wlEntry.dataMask; |
| 1150 | // Skip as no-match, if requested write data is more than the |
| 1151 | // write data mask size |
| 1152 | if (writeData.size() > dataMask.size()) |
| 1153 | { |
| 1154 | continue; |
| 1155 | } |
| 1156 | if (isWriteDataWhitelisted(wlEntry.data, dataMask, writeData)) |
| 1157 | { |
| 1158 | return true; |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | return false; |
| 1163 | } |
| 1164 | |
| 1165 | /** @brief implements master write read IPMI command which can be used for |
| 1166 | * low-level I2C/SMBus write, read or write-read access |
| 1167 | * @param isPrivateBus -to indicate private bus usage |
| 1168 | * @param busId - bus id |
| 1169 | * @param channelNum - channel number |
| 1170 | * @param reserved - skip 1 bit |
| 1171 | * @param slaveAddr - slave address |
| 1172 | * @param read count - number of bytes to be read |
| 1173 | * @param writeData - data to be written |
| 1174 | * |
| 1175 | * @returns IPMI completion code plus response data |
| 1176 | * - readData - i2c response data |
| 1177 | */ |
| 1178 | ipmi::RspType<std::vector<uint8_t>> |
| 1179 | ipmiMasterWriteRead(bool isPrivateBus, uint3_t busId, uint4_t channelNum, |
| 1180 | bool reserved, uint7_t slaveAddr, uint8_t readCount, |
| 1181 | std::vector<uint8_t> writeData) |
| 1182 | { |
| 1183 | i2c_rdwr_ioctl_data msgReadWrite = {0}; |
| 1184 | i2c_msg i2cmsg[2] = {0}; |
| 1185 | |
| 1186 | if (readCount > maxIPMIWriteReadSize) |
| 1187 | { |
| 1188 | log<level::ERR>("Master write read command: Read count exceeds limit"); |
| 1189 | return ipmi::responseParmOutOfRange(); |
| 1190 | } |
| 1191 | const size_t writeCount = writeData.size(); |
| 1192 | if (!readCount && !writeCount) |
| 1193 | { |
| 1194 | log<level::ERR>("Master write read command: Read & write count are 0"); |
| 1195 | return ipmi::responseInvalidFieldRequest(); |
| 1196 | } |
| 1197 | if (!isCmdWhitelisted(static_cast<uint8_t>(busId), |
| 1198 | static_cast<uint8_t>(slaveAddr), writeData)) |
| 1199 | { |
| 1200 | log<level::ERR>("Master write read request blocked!", |
| 1201 | entry("BUS=%d", static_cast<uint8_t>(busId)), |
| 1202 | entry("ADDR=0x%x", static_cast<uint8_t>(slaveAddr))); |
| 1203 | return ipmi::responseInvalidFieldRequest(); |
| 1204 | } |
| 1205 | std::vector<uint8_t> readBuf(readCount); |
| 1206 | std::string i2cBus = |
| 1207 | "/dev/i2c-" + std::to_string(static_cast<uint8_t>(busId)); |
| 1208 | |
| 1209 | int i2cDev = ::open(i2cBus.c_str(), O_RDWR | O_CLOEXEC); |
| 1210 | if (i2cDev < 0) |
| 1211 | { |
| 1212 | log<level::ERR>("Failed to open i2c bus", |
| 1213 | entry("BUS=%s", i2cBus.c_str())); |
| 1214 | return ipmi::responseInvalidFieldRequest(); |
| 1215 | } |
| 1216 | |
| 1217 | int msgCount = 0; |
| 1218 | if (writeCount) |
| 1219 | { |
| 1220 | i2cmsg[msgCount].addr = static_cast<uint8_t>(slaveAddr); |
| 1221 | i2cmsg[msgCount].flags = 0x00; |
| 1222 | i2cmsg[msgCount].len = writeCount; |
| 1223 | i2cmsg[msgCount].buf = writeData.data(); |
| 1224 | msgCount++; |
| 1225 | } |
| 1226 | if (readCount) |
| 1227 | { |
| 1228 | i2cmsg[msgCount].addr = static_cast<uint8_t>(slaveAddr); |
| 1229 | i2cmsg[msgCount].flags = I2C_M_RD; |
| 1230 | i2cmsg[msgCount].len = readCount; |
| 1231 | i2cmsg[msgCount].buf = readBuf.data(); |
| 1232 | msgCount++; |
| 1233 | } |
| 1234 | |
| 1235 | msgReadWrite.msgs = i2cmsg; |
| 1236 | msgReadWrite.nmsgs = msgCount; |
| 1237 | |
| 1238 | int ret = ::ioctl(i2cDev, I2C_RDWR, &msgReadWrite); |
| 1239 | ::close(i2cDev); |
| 1240 | |
| 1241 | if (ret < 0) |
| 1242 | { |
| 1243 | log<level::ERR>("Master write read: Failed", entry("RET=%d", ret)); |
| 1244 | return ipmi::responseUnspecifiedError(); |
| 1245 | } |
| 1246 | if (readCount) |
| 1247 | { |
| 1248 | readBuf.resize(msgReadWrite.msgs[msgCount - 1].len); |
| 1249 | } |
| 1250 | return ipmi::responseSuccess(readBuf); |
| 1251 | } |
| 1252 | |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 1253 | void register_netfn_app_functions() |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 1254 | { |
Vernon Mauery | 86a5082 | 2019-03-25 13:11:36 -0700 | [diff] [blame] | 1255 | // <Get Device ID> |
| 1256 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, |
| 1257 | ipmi::app::cmdGetDeviceId, ipmi::Privilege::User, |
| 1258 | ipmiAppGetDeviceId); |
| 1259 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1260 | // <Get BT Interface Capabilities> |
Vernon Mauery | cc99ba4 | 2019-03-25 13:40:11 -0700 | [diff] [blame] | 1261 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, |
| 1262 | ipmi::app::cmdGetBtIfaceCapabilities, |
| 1263 | ipmi::Privilege::User, ipmiAppGetBtCapabilities); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 1264 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1265 | // <Reset Watchdog Timer> |
Vernon Mauery | 11df4f6 | 2019-03-25 14:17:54 -0700 | [diff] [blame] | 1266 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, |
| 1267 | ipmi::app::cmdResetWatchdogTimer, |
| 1268 | ipmi::Privilege::Operator, ipmiAppResetWatchdogTimer); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 1269 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1270 | // <Set Watchdog Timer> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1271 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, |
| 1272 | ipmi_app_watchdog_set, PRIVILEGE_OPERATOR); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 1273 | |
William A. Kennington III | 73f4451 | 2018-02-09 15:28:46 -0800 | [diff] [blame] | 1274 | // <Get Watchdog Timer> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1275 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_WD, NULL, |
| 1276 | ipmi_app_watchdog_get, PRIVILEGE_OPERATOR); |
William A. Kennington III | 73f4451 | 2018-02-09 15:28:46 -0800 | [diff] [blame] | 1277 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1278 | // <Get Self Test Results> |
Vernon Mauery | b84a528 | 2019-03-25 13:39:03 -0700 | [diff] [blame] | 1279 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, |
| 1280 | ipmi::app::cmdGetSelfTestResults, |
| 1281 | ipmi::Privilege::User, ipmiAppGetSelfTestResults); |
Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 1282 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1283 | // <Get Device GUID> |
Vernon Mauery | 1554132 | 2019-03-25 13:33:03 -0700 | [diff] [blame] | 1284 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, |
| 1285 | ipmi::app::cmdGetDeviceGuid, ipmi::Privilege::User, |
| 1286 | ipmiAppGetDeviceGuid); |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 1287 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1288 | // <Set ACPI Power State> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1289 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, |
| 1290 | ipmi_app_set_acpi_power_state, PRIVILEGE_ADMIN); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 1291 | |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 1292 | // <Get ACPI Power State> |
Deepak Kumar Sahu | 4e6d257 | 2019-05-07 19:53:37 +0000 | [diff] [blame] | 1293 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, |
| 1294 | ipmi::app::cmdGetAcpiPowerState, |
| 1295 | ipmi::Privilege::Admin, ipmiGetAcpiPowerState); |
Yong Li | 18d7726 | 2018-10-09 01:59:45 +0800 | [diff] [blame] | 1296 | |
Richard Marian Thomaiyar | 84bf9be | 2019-04-26 22:59:16 +0530 | [diff] [blame] | 1297 | // Note: For security reason, this command will be registered only when |
| 1298 | // there are proper I2C Master write read whitelist |
| 1299 | if (populateI2CMasterWRWhitelist()) |
| 1300 | { |
| 1301 | // Note: For security reasons, registering master write read as admin |
| 1302 | // privilege command, even though IPMI 2.0 specification allows it as |
| 1303 | // operator privilege. |
| 1304 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, |
| 1305 | ipmi::app::cmdMasterWriteRead, |
| 1306 | ipmi::Privilege::Admin, ipmiMasterWriteRead); |
| 1307 | } |
| 1308 | |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 1309 | // <Get System GUID Command> |
Vernon Mauery | b90a532 | 2019-03-25 13:36:55 -0700 | [diff] [blame] | 1310 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp, |
| 1311 | ipmi::app::cmdGetSystemGuid, ipmi::Privilege::User, |
| 1312 | ipmiAppGetSystemGuid); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 1313 | |
| 1314 | // <Get Channel Cipher Suites Command> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1315 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_CIPHER_SUITES, NULL, |
| 1316 | getChannelCipherSuites, PRIVILEGE_CALLBACK); |
Vernon Mauery | d2a57de | 2019-03-25 12:45:28 -0700 | [diff] [blame] | 1317 | |
Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 1318 | // <Get System Info Command> |
| 1319 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SYSTEM_INFO, NULL, |
| 1320 | ipmi_app_get_system_info, PRIVILEGE_USER); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 1321 | return; |
| 1322 | } |