Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "oemcommands.hpp" |
| 18 | |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 19 | #include <boost/algorithm/string.hpp> |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 20 | #include <ipmid/api.hpp> |
| 21 | #include <ipmid/utils.hpp> |
| 22 | #include <phosphor-logging/log.hpp> |
James Feist | fcd2d3a | 2020-05-28 10:38:15 -0700 | [diff] [blame] | 23 | |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 24 | #include <variant> |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 25 | |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 26 | namespace ipmi |
| 27 | { |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 28 | void register_netfn_bmc_control_functions() __attribute__((constructor)); |
| 29 | |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 30 | static constexpr uint8_t rmcpServiceBitPos = 3; |
| 31 | static constexpr uint8_t webServiceBitPos = 5; |
| 32 | static constexpr uint8_t solServiceBitPos = 6; |
| 33 | static constexpr uint8_t kvmServiceBitPos = 15; |
| 34 | |
| 35 | static const std::unordered_map<uint8_t, std::string> bmcServices = { |
| 36 | // {bit position for service, service object path} |
| 37 | {rmcpServiceBitPos, |
| 38 | "/xyz/openbmc_project/control/service/phosphor_2dipmi_2dnet"}, |
| 39 | {webServiceBitPos, "/xyz/openbmc_project/control/service/bmcweb"}, |
Jiaqing Zhao | c9290e7 | 2022-01-05 12:57:11 +0800 | [diff] [blame] | 40 | {solServiceBitPos, |
| 41 | "/xyz/openbmc_project/control/service/obmc_2dconsole_2dssh"}, |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 42 | {kvmServiceBitPos, "/xyz/openbmc_project/control/service/start_2dipkvm"}, |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Arun P. Mohanan | 98cb618 | 2021-08-31 19:49:19 +0530 | [diff] [blame] | 45 | static constexpr uint16_t maskBit15 = 0x8000; |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 46 | |
| 47 | static constexpr const char* objectManagerIntf = |
| 48 | "org.freedesktop.DBus.ObjectManager"; |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 49 | static constexpr const char* dBusPropIntf = "org.freedesktop.DBus.Properties"; |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 50 | static constexpr const char* serviceConfigBasePath = |
| 51 | "/xyz/openbmc_project/control/service"; |
| 52 | static constexpr const char* serviceConfigAttrIntf = |
| 53 | "xyz.openbmc_project.Control.Service.Attributes"; |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 54 | static constexpr const char* getMgdObjMethod = "GetManagedObjects"; |
| 55 | static constexpr const char* propMasked = "Masked"; |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 56 | |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 57 | std::string getServiceConfigMgrName() |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 58 | { |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 59 | static std::string serviceCfgMgr{}; |
| 60 | if (serviceCfgMgr.empty()) |
| 61 | { |
| 62 | try |
| 63 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 64 | auto sdbusp = getSdBus(); |
| 65 | serviceCfgMgr = ipmi::getService(*sdbusp, objectManagerIntf, |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 66 | serviceConfigBasePath); |
| 67 | } |
Patrick Williams | 69245b7 | 2021-09-02 09:23:01 -0500 | [diff] [blame] | 68 | catch (const sdbusplus::exception::exception& e) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 69 | { |
| 70 | serviceCfgMgr.clear(); |
| 71 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 72 | "Error: In fetching disabling service manager name"); |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 73 | return serviceCfgMgr; |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 76 | return serviceCfgMgr; |
| 77 | } |
| 78 | |
| 79 | static inline void checkAndThrowError(boost::system::error_code& ec, |
| 80 | const std::string& msg) |
| 81 | { |
| 82 | if (ec) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 83 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 84 | std::string msgToLog = ec.message() + (msg.empty() ? "" : " - " + msg); |
| 85 | phosphor::logging::log<phosphor::logging::level::ERR>(msgToLog.c_str()); |
| 86 | throw sdbusplus::exception::SdBusError(-EIO, msgToLog.c_str()); |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 87 | } |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | |
| 91 | static inline bool getEnabledValue(const DbusInterfaceMap& intfMap) |
| 92 | { |
| 93 | for (const auto& intf : intfMap) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 94 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 95 | if (intf.first == serviceConfigAttrIntf) |
| 96 | { |
| 97 | auto it = intf.second.find(propMasked); |
| 98 | if (it == intf.second.end()) |
| 99 | { |
| 100 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 101 | "Error: in getting Masked property value"); |
| 102 | throw sdbusplus::exception::SdBusError( |
| 103 | -EIO, "ERROR in reading Masked property value"); |
| 104 | } |
| 105 | // return !Masked value |
| 106 | return !std::get<bool>(it->second); |
| 107 | } |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 108 | } |
James Feist | 13b0039 | 2019-08-19 16:16:08 -0700 | [diff] [blame] | 109 | return false; |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 112 | ipmi::RspType<> setBmcControlServices(boost::asio::yield_context yield, |
| 113 | uint8_t state, uint16_t serviceValue) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 114 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 115 | constexpr uint16_t servicesRsvdMask = 0x3F97; |
| 116 | constexpr uint8_t enableService = 0x1; |
| 117 | |
| 118 | if ((state > enableService) || (serviceValue & servicesRsvdMask) || |
| 119 | !serviceValue) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 120 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 121 | return ipmi::responseInvalidFieldRequest(); |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 122 | } |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 123 | try |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 124 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 125 | auto sdbusp = getSdBus(); |
| 126 | boost::system::error_code ec; |
| 127 | auto objectMap = sdbusp->yield_method_call<ObjectValueTree>( |
| 128 | yield, ec, getServiceConfigMgrName().c_str(), serviceConfigBasePath, |
| 129 | objectManagerIntf, getMgdObjMethod); |
| 130 | checkAndThrowError(ec, "GetMangagedObjects for service cfg failed"); |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 131 | |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 132 | for (const auto& services : bmcServices) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 133 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 134 | // services.first holds the bit position of the service, check |
| 135 | // whether it has to be updated. |
| 136 | const uint16_t serviceMask = 1 << services.first; |
| 137 | if (!(serviceValue & serviceMask)) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 138 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 139 | continue; |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 140 | } |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 141 | for (const auto& obj : objectMap) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 142 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 143 | if (boost::algorithm::starts_with(obj.first.str, |
| 144 | services.second)) |
| 145 | { |
| 146 | if (state != getEnabledValue(obj.second)) |
| 147 | { |
| 148 | ec.clear(); |
| 149 | sdbusp->yield_method_call<>( |
| 150 | yield, ec, getServiceConfigMgrName().c_str(), |
| 151 | obj.first.str, dBusPropIntf, "Set", |
| 152 | serviceConfigAttrIntf, propMasked, |
| 153 | std::variant<bool>(!state)); |
| 154 | checkAndThrowError(ec, "Set Masked property failed"); |
| 155 | // Multiple instances may be present, so continue |
| 156 | } |
| 157 | } |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
Patrick Williams | bd51e6a | 2021-10-06 13:09:44 -0500 | [diff] [blame] | 161 | catch (const sdbusplus::exception::exception& e) |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 162 | { |
| 163 | return ipmi::responseUnspecifiedError(); |
| 164 | } |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 165 | return ipmi::responseSuccess(); |
| 166 | } |
| 167 | |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 168 | ipmi::RspType<uint16_t> getBmcControlServices(boost::asio::yield_context yield) |
| 169 | { |
| 170 | uint16_t serviceValue = 0; |
| 171 | try |
| 172 | { |
| 173 | auto sdbusp = getSdBus(); |
| 174 | boost::system::error_code ec; |
| 175 | auto objectMap = sdbusp->yield_method_call<ObjectValueTree>( |
| 176 | yield, ec, getServiceConfigMgrName().c_str(), serviceConfigBasePath, |
| 177 | objectManagerIntf, getMgdObjMethod); |
| 178 | checkAndThrowError(ec, "GetMangagedObjects for service cfg failed"); |
| 179 | |
| 180 | for (const auto& services : bmcServices) |
| 181 | { |
| 182 | for (const auto& obj : objectMap) |
| 183 | { |
| 184 | if (boost::algorithm::starts_with(obj.first.str, |
| 185 | services.second)) |
| 186 | { |
| 187 | serviceValue |= getEnabledValue(obj.second) |
| 188 | << services.first; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
Patrick Williams | bd51e6a | 2021-10-06 13:09:44 -0500 | [diff] [blame] | 194 | catch (const sdbusplus::exception::exception& e) |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 195 | { |
| 196 | return ipmi::responseUnspecifiedError(); |
| 197 | } |
| 198 | // Bit 14 should match bit 15 as single service maintains Video & USB |
| 199 | // redirection |
| 200 | serviceValue |= (serviceValue & maskBit15) >> 1; |
| 201 | return ipmi::responseSuccess(serviceValue); |
| 202 | } |
| 203 | |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 204 | void register_netfn_bmc_control_functions() |
| 205 | { |
Vernon Mauery | 98bbf69 | 2019-09-16 11:14:59 -0700 | [diff] [blame] | 206 | registerHandler(prioOpenBmcBase, intel::netFnGeneral, |
| 207 | intel::general::cmdControlBmcServices, Privilege::Admin, |
| 208 | setBmcControlServices); |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 209 | |
Vernon Mauery | 98bbf69 | 2019-09-16 11:14:59 -0700 | [diff] [blame] | 210 | registerHandler(prioOpenBmcBase, intel::netFnGeneral, |
| 211 | intel::general::cmdGetBmcServiceStatus, Privilege::User, |
| 212 | getBmcControlServices); |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 213 | } |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 214 | } // namespace ipmi |