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 | |
Jiaqing Zhao | 95f6933 | 2022-06-04 15:09:45 +0800 | [diff] [blame] | 35 | static constexpr std::array<std::pair<uint8_t, const char*>, 4> bmcServices = {{ |
| 36 | // {bit position for service, service name} |
| 37 | {rmcpServiceBitPos, "phosphor-ipmi-net"}, |
| 38 | {webServiceBitPos, "bmcweb"}, |
| 39 | {solServiceBitPos, "obmc-console-ssh"}, |
| 40 | {kvmServiceBitPos, "start-ipkvm"}, |
| 41 | }}; |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 42 | |
Arun P. Mohanan | 98cb618 | 2021-08-31 19:49:19 +0530 | [diff] [blame] | 43 | static constexpr uint16_t maskBit15 = 0x8000; |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 44 | |
| 45 | static constexpr const char* objectManagerIntf = |
| 46 | "org.freedesktop.DBus.ObjectManager"; |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 47 | static constexpr const char* dBusPropIntf = "org.freedesktop.DBus.Properties"; |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 48 | static constexpr const char* serviceConfigBasePath = |
| 49 | "/xyz/openbmc_project/control/service"; |
| 50 | static constexpr const char* serviceConfigAttrIntf = |
| 51 | "xyz.openbmc_project.Control.Service.Attributes"; |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 52 | static constexpr const char* getMgdObjMethod = "GetManagedObjects"; |
| 53 | static constexpr const char* propMasked = "Masked"; |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 54 | |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 55 | std::string getServiceConfigMgrName() |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 56 | { |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 57 | static std::string serviceCfgMgr{}; |
| 58 | if (serviceCfgMgr.empty()) |
| 59 | { |
| 60 | try |
| 61 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 62 | auto sdbusp = getSdBus(); |
| 63 | serviceCfgMgr = ipmi::getService(*sdbusp, objectManagerIntf, |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 64 | serviceConfigBasePath); |
| 65 | } |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 66 | catch (const sdbusplus::exception_t& e) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 67 | { |
| 68 | serviceCfgMgr.clear(); |
| 69 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 70 | "Error: In fetching disabling service manager name"); |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 71 | return serviceCfgMgr; |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 74 | return serviceCfgMgr; |
| 75 | } |
| 76 | |
| 77 | static inline void checkAndThrowError(boost::system::error_code& ec, |
| 78 | const std::string& msg) |
| 79 | { |
| 80 | if (ec) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 81 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 82 | std::string msgToLog = ec.message() + (msg.empty() ? "" : " - " + msg); |
| 83 | phosphor::logging::log<phosphor::logging::level::ERR>(msgToLog.c_str()); |
| 84 | throw sdbusplus::exception::SdBusError(-EIO, msgToLog.c_str()); |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 85 | } |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | |
| 89 | static inline bool getEnabledValue(const DbusInterfaceMap& intfMap) |
| 90 | { |
| 91 | for (const auto& intf : intfMap) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 92 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 93 | if (intf.first == serviceConfigAttrIntf) |
| 94 | { |
| 95 | auto it = intf.second.find(propMasked); |
| 96 | if (it == intf.second.end()) |
| 97 | { |
| 98 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 99 | "Error: in getting Masked property value"); |
| 100 | throw sdbusplus::exception::SdBusError( |
| 101 | -EIO, "ERROR in reading Masked property value"); |
| 102 | } |
| 103 | // return !Masked value |
| 104 | return !std::get<bool>(it->second); |
| 105 | } |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 106 | } |
James Feist | 13b0039 | 2019-08-19 16:16:08 -0700 | [diff] [blame] | 107 | return false; |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 110 | ipmi::RspType<> setBmcControlServices(boost::asio::yield_context yield, |
| 111 | uint8_t state, uint16_t serviceValue) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 112 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 113 | constexpr uint16_t servicesRsvdMask = 0x3F97; |
| 114 | constexpr uint8_t enableService = 0x1; |
| 115 | |
| 116 | if ((state > enableService) || (serviceValue & servicesRsvdMask) || |
| 117 | !serviceValue) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 118 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 119 | return ipmi::responseInvalidFieldRequest(); |
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 | try |
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 | auto sdbusp = getSdBus(); |
| 124 | boost::system::error_code ec; |
| 125 | auto objectMap = sdbusp->yield_method_call<ObjectValueTree>( |
| 126 | yield, ec, getServiceConfigMgrName().c_str(), serviceConfigBasePath, |
| 127 | objectManagerIntf, getMgdObjMethod); |
| 128 | checkAndThrowError(ec, "GetMangagedObjects for service cfg failed"); |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 129 | |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 130 | for (const auto& services : bmcServices) |
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 | // services.first holds the bit position of the service, check |
| 133 | // whether it has to be updated. |
| 134 | const uint16_t serviceMask = 1 << services.first; |
| 135 | if (!(serviceValue & serviceMask)) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 136 | { |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 137 | continue; |
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 | for (const auto& obj : objectMap) |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 140 | { |
Jiaqing Zhao | 95f6933 | 2022-06-04 15:09:45 +0800 | [diff] [blame] | 141 | if (boost::algorithm::starts_with(obj.first.filename(), |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 142 | services.second)) |
| 143 | { |
| 144 | if (state != getEnabledValue(obj.second)) |
| 145 | { |
| 146 | ec.clear(); |
| 147 | sdbusp->yield_method_call<>( |
| 148 | yield, ec, getServiceConfigMgrName().c_str(), |
| 149 | obj.first.str, dBusPropIntf, "Set", |
| 150 | serviceConfigAttrIntf, propMasked, |
| 151 | std::variant<bool>(!state)); |
| 152 | checkAndThrowError(ec, "Set Masked property failed"); |
| 153 | // Multiple instances may be present, so continue |
| 154 | } |
| 155 | } |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 159 | catch (const sdbusplus::exception_t& e) |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 160 | { |
| 161 | return ipmi::responseUnspecifiedError(); |
| 162 | } |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 163 | return ipmi::responseSuccess(); |
| 164 | } |
| 165 | |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 166 | ipmi::RspType<uint16_t> getBmcControlServices(boost::asio::yield_context yield) |
| 167 | { |
| 168 | uint16_t serviceValue = 0; |
| 169 | try |
| 170 | { |
| 171 | auto sdbusp = getSdBus(); |
| 172 | boost::system::error_code ec; |
| 173 | auto objectMap = sdbusp->yield_method_call<ObjectValueTree>( |
| 174 | yield, ec, getServiceConfigMgrName().c_str(), serviceConfigBasePath, |
| 175 | objectManagerIntf, getMgdObjMethod); |
| 176 | checkAndThrowError(ec, "GetMangagedObjects for service cfg failed"); |
| 177 | |
| 178 | for (const auto& services : bmcServices) |
| 179 | { |
| 180 | for (const auto& obj : objectMap) |
| 181 | { |
Jiaqing Zhao | 95f6933 | 2022-06-04 15:09:45 +0800 | [diff] [blame] | 182 | if (boost::algorithm::starts_with(obj.first.filename(), |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 183 | services.second)) |
| 184 | { |
| 185 | serviceValue |= getEnabledValue(obj.second) |
| 186 | << services.first; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
Patrick Williams | f944d2e | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 192 | catch (const sdbusplus::exception_t& e) |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 193 | { |
| 194 | return ipmi::responseUnspecifiedError(); |
| 195 | } |
| 196 | // Bit 14 should match bit 15 as single service maintains Video & USB |
| 197 | // redirection |
| 198 | serviceValue |= (serviceValue & maskBit15) >> 1; |
| 199 | return ipmi::responseSuccess(serviceValue); |
| 200 | } |
| 201 | |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 202 | void register_netfn_bmc_control_functions() |
| 203 | { |
Vernon Mauery | 98bbf69 | 2019-09-16 11:14:59 -0700 | [diff] [blame] | 204 | registerHandler(prioOpenBmcBase, intel::netFnGeneral, |
| 205 | intel::general::cmdControlBmcServices, Privilege::Admin, |
| 206 | setBmcControlServices); |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 207 | |
Vernon Mauery | 98bbf69 | 2019-09-16 11:14:59 -0700 | [diff] [blame] | 208 | registerHandler(prioOpenBmcBase, intel::netFnGeneral, |
| 209 | intel::general::cmdGetBmcServiceStatus, Privilege::User, |
| 210 | getBmcControlServices); |
Vernon Mauery | a3702c1 | 2019-05-22 13:20:59 -0700 | [diff] [blame] | 211 | } |
Richard Marian Thomaiyar | edf25e6 | 2019-06-11 01:14:49 +0530 | [diff] [blame] | 212 | } // namespace ipmi |