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